#P12544. [UOI 2025] Boys and Girls

[UOI 2025] Boys and Girls

题目描述

There are nn types of boys and 2n2 \cdot n girls. The types of boys are numbered with integers from 11 to nn, while the girls are numbered with integers from 11 to 2n2 \cdot n.

There are cic_i boys of the ii-th type, and each of them likes girls numbered aia_i and bib_i.

Find the size of the largest set of boys such that for every pair of boys in this set, there is at least one girl that both of them like.

In this problem, each test contains several sets of input data. You need to solve the problem independently for each such set.

输入格式

The first line contains one integer TT (1T500)(1 \le T \le 500) --- the number of sets of input data. The description of the input data sets follows.

In the first line of each input data set, there is one integer nn (1n7105)(1 \le n \le 7 \cdot 10^5).

In the next nn lines, there are three integers aia_i, bib_i, cic_i $(1 \le a_i < b_i \le 2 \cdot n, 1 \le c_i \le 10^9)$ --- the parameters for the corresponding type of boys.

It is guaranteed that aiaja_i \ne a_j or bibjb_i \ne b_j for any 1i<jn1 \le i < j \le n.

It is guaranteed that the sum of nn across all input data sets of a single test does not exceed 71057 \cdot 10^5.

输出格式

For each set of input data, output one integer on a separate line --- the size of the largest set of boys such that for every pair of boys in this set, there is at least one girl that both of them like.

3
2
1 2 3
3 4 5
5
1 2 1
1 3 4
4 5 2
3 4 2
1 4 3
4
1 2 3
2 3 4
3 5 4
1 3 2
5
9
10

提示

Let SS be the sum of nn over all test case input sets of one test, and KK be the sum of all cic_i over all test case input sets of one test.

  • (55 points): n5n \le 5;
  • (1111 points): S100S \le 100;
  • (77 points): each girl is liked by boys of no more than two types;
  • (1010 points): S3000S \le 3000;
  • (2323 points): S3105S \le 3 \cdot 10^5;
  • (1919 points): K107K \le 10^7;
  • (2525 points): with no additional constraints.