QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#884264#10066. Many Pairsguleng2007#12 1ms3840kbC++201.1kb2025-02-05 23:04:422025-02-05 23:04:42

Judging History

This is the latest submission verdict.

  • [2025-02-05 23:04:42]
  • Judged
  • Verdict: 12
  • Time: 1ms
  • Memory: 3840kb
  • [2025-02-05 23:04:42]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

const int N=5005;

vector <int> g[N];
int a[N], b[N], c[N];
long long ans[N];
bool have[N];
vector <int> vec;

void dfs(int x,int fa)
{
	vec.push_back(x);
	for(auto to:g[x])
		if(to!=fa)
			dfs(to,x);
}

int main()
{
	int n,m;
	cin >> n >> m;
	for(int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d %d",&x,&y);
		g[x].push_back(y);
		g[y].push_back(x);
	}

	long long sum=0;
	for(int i=1;i<=m;i++)
	{
		scanf("%d %d %d",&a[i],&b[i],&c[i]);
		sum += c[i];
	}

	for(int i=1;i<=n;i++)
	{
		if(g[i].size()<=1)
			ans[i]=sum;
		else
		{
			for(auto to1:g[i])
				for(auto to2:g[i])
					if(to1!=to2)
					{
						vec.clear();
						vec.push_back(i);
						dfs(to1,i);
						dfs(to2,i);

						memset(have,0,sizeof(have));
						for(auto x:vec)
							have[x]=true;

						long long tot=0;
						for(int i=1;i<=m;i++)
							if(have[a[i]] && have[b[i]])
								tot += c[i];
						ans[i]=max(ans[i],tot);
					}
		}
	}

	for(int i=1;i<=n;i++)
		printf("%lld ",ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 12
Accepted

Test #1:

score: 12
Accepted
time: 1ms
memory: 3840kb

input:

50 50
1 20
13 1
39 1
12 1
16 1
4 1
9 1
1 17
33 1
1 28
1 46
31 1
1 23
44 1
15 1
1 7
14 1
1 40
1 25
1 34
11 1
6 1
1 30
1 3
42 1
1 48
35 1
27 1
1 45
29 1
10 1
19 1
2 1
38 1
41 1
1 37
1 32
18 1
47 1
26 1
22 1
1 5
1 50
49 1
1 21
43 1
1 8
1 36
24 1
26 20 640
41 45 742
39 15 520
20 18 703
47 16 391
2 19 97...

output:

1864 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 

result:

ok single line: '1864 35112 35112 35112 35112 3... 35112 35112 35112 35112 35112 '

Test #2:

score: 12
Accepted
time: 0ms
memory: 3840kb

input:

50 50
50 1
28 1
1 15
16 1
1 38
5 1
1 23
1 42
37 1
39 1
1 4
32 1
10 1
18 1
1 19
1 36
46 1
1 34
20 1
2 1
1 26
44 1
24 1
1 30
1 43
17 1
7 1
12 1
1 22
29 1
1 3
31 1
33 1
1 14
1 49
48 1
13 1
1 45
25 1
35 1
1 41
1 21
8 1
27 1
9 1
1 40
1 6
47 1
1 11
14 19 680
44 37 476
39 12 463
16 7 354
49 41 430
38 42 77...

output:

1250 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 

result:

ok single line: '1250 32887 32887 32887 32887 3... 32887 32887 32887 32887 32887 '

Test #3:

score: 12
Accepted
time: 0ms
memory: 3840kb

input:

50 50
1 31
17 1
33 1
13 1
32 30
34 1
43 36
1 14
10 1
1 8
44 4
7 1
21 48
1 22
1 23
15 1
37 1
1 24
1 42
1 9
47 1
1 4
48 1
1 41
32 1
3 4
27 1
11 4
32 50
46 4
1 28
18 1
49 1
6 4
17 29
4 40
25 1
16 48
1 45
1 36
12 4
4 2
32 20
19 38
32 39
1 5
1 19
1 35
26 1
13 15 637
22 40 788
42 39 443
48 19 415
14 39 45...

output:

3540 31749 31749 23997 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31385 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31445 31749 31749 

result:

ok single line: '3540 31749 31749 23997 31749 3... 31749 31749 31445 31749 31749 '

Test #4:

score: 12
Accepted
time: 0ms
memory: 3840kb

input:

50 50
37 31
17 9
33 21
13 36
10 30
34 39
43 28
11 14
10 42
23 8
44 41
7 19
21 24
30 22
17 23
15 24
37 20
38 24
37 42
26 9
47 11
1 4
48 32
7 41
32 4
3 39
27 38
11 21
37 50
46 14
6 28
18 44
49 8
6 11
47 29
5 40
25 30
16 39
6 45
48 36
12 2
28 2
23 20
34 38
5 39
30 5
13 19
18 35
26 18
13 15 637
22 40 78...

output:

31749 31749 31749 31749 28545 29418 31749 31749 31749 31749 29434 31749 31749 31749 31749 31749 31749 30513 31749 31749 31041 31749 26850 29829 31749 31749 31749 30766 31749 29541 31749 31749 31749 31749 31749 31749 31189 30356 30338 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 31749 

result:

ok single line: '31749 31749 31749 31749 28545 ... 31749 31749 31749 31749 31749 '

Test #5:

score: 12
Accepted
time: 1ms
memory: 3840kb

input:

50 50
50 26
28 38
21 15
16 1
36 38
5 45
19 23
10 42
37 6
39 27
20 4
32 48
10 28
18 23
29 19
18 36
46 11
8 34
20 46
2 39
47 26
44 16
24 9
31 30
35 43
17 44
7 5
12 4
12 22
29 22
25 3
31 9
33 34
43 14
15 49
48 33
13 42
13 45
25 7
35 3
14 41
2 21
8 40
27 30
9 50
17 40
41 6
47 37
32 11
14 19 680
44 37 47...

output:

32887 32887 32887 32887 32887 32887 32887 32887 32483 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 32887 

result:

ok single line: '32887 32887 32887 32887 32887 ... 32887 32887 32887 32887 32887 '

Test #6:

score: 12
Accepted
time: 1ms
memory: 3840kb

input:

50 50
15 20
13 17
39 43
12 50
16 27
4 45
9 42
34 17
33 44
24 28
32 46
31 5
38 23
44 26
15 35
40 7
14 13
6 40
30 25
37 34
11 29
6 49
7 30
2 3
42 10
47 48
35 8
27 22
20 45
29 25
10 3
19 23
2 19
38 31
41 14
4 37
18 32
18 48
47 11
26 9
22 12
46 5
36 50
49 1
16 21
43 41
28 8
33 36
24 21
26 20 640
41 45 7...

output:

35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 35112 

result:

ok single line: '35112 35112 35112 35112 35112 ... 35112 35112 35112 35112 35112 '

Test #7:

score: 12
Accepted
time: 1ms
memory: 3840kb

input:

50 50
8 1
34 3
1 34
41 11
32 20
13 32
44 1
9 1
2 46
43 1
34 36
38 43
8 47
29 43
18 34
2 12
41 14
43 35
39 23
10 42
30 9
42 28
33 2
8 4
39 25
7 22
1 22
22 27
2 1
45 39
40 22
48 42
2 37
26 39
1 42
22 24
42 5
43 31
39 1
1 41
17 39
16 9
49 34
21 9
22 50
32 1
8 19
15 32
41 6
31 47 905
35 20 568
10 19 956...

output:

3187 27989 30442 30442 30442 30442 30442 28534 27565 30442 30442 30442 30442 30442 30442 30442 30442 30442 30442 30442 30442 28692 30442 30442 30442 30442 30442 30442 30442 30442 30442 28374 30442 29539 30442 30442 30442 30442 25323 30442 30004 28854 25759 30442 30442 30442 30442 30442 30442 30442 

result:

ok single line: '3187 27989 30442 30442 30442 3... 30442 30442 30442 30442 30442 '

Test #8:

score: 12
Accepted
time: 1ms
memory: 3840kb

input:

50 50
50 9
38 4
3 40
14 1
7 21
27 10
29 44
47 9
25 32
15 49
12 44
23 30
11 50
25 28
35 45
36 46
32 29
37 2
12 20
5 41
45 30
37 14
17 8
28 6
3 11
1 4
24 39
21 16
35 10
34 24
48 16
41 15
40 23
43 34
43 22
31 20
7 42
19 1
39 31
36 8
19 6
46 18
48 18
5 22
38 13
17 33
26 42
2 26
13 47
3 21 797
30 28 341
...

output:

20937 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 33754 

result:

ok single line: '20937 33754 33754 33754 33754 ... 33754 33754 33754 33754 33754 '

Subtask #2:

score: 0
Time Limit Exceeded

Test #9:

score: 0
Time Limit Exceeded

input:

5000 500
1 2035
2103 1
2889 1
3899 1
1 1572
2838 1
1 3608
2582 1
1 2880
201 1
3989 1
1 1300
4071 1
2576 4504
1 3547
1 1145
3878 1
1 3240
1 2047
1 4983
3564 1
4965 4504
1 1305
3217 1
1 4820
2377 1
1640 1
1363 1
1 4731
1 4040
1 3707
3364 1
1 79
1 4968
2679 1
1 3276
1 4709
1 4438
1 3420
1 828
4504 4554...

output:


result:


Subtask #3:

score: 0
Time Limit Exceeded

Test #15:

score: 0
Time Limit Exceeded

input:

5000 2000
1 2035
2103 1
2889 1
3899 1
1 1572
2838 1
1 3608
2582 1
1 2880
201 1
3989 1
1 1300
4071 1
2576 4504
1 3547
1 1145
3878 1
1 3240
1 2047
1 4983
3564 1
4965 4504
1 1305
3217 1
1 4820
2377 1
1640 1
1363 1
1 4731
1 4040
1 3707
3364 1
1 79
1 4968
2679 1
1 3276
1 4709
1 4438
1 3420
1 828
4504 455...

output:


result:


Subtask #4:

score: 0
Time Limit Exceeded

Test #21:

score: 0
Time Limit Exceeded

input:

5000 5000
1 2035
2103 1
2889 1
3899 1
1 1572
2838 1
1 3608
2582 1
1 2880
201 1
3989 1
1 1300
4071 1
2576 4504
1 3547
1 1145
3878 1
1 3240
1 2047
1 4983
3564 1
4965 4504
1 1305
3217 1
1 4820
2377 1
1640 1
1363 1
1 4731
1 4040
1 3707
3364 1
1 79
1 4968
2679 1
1 3276
1 4709
1 4438
1 3420
1 828
4504 455...

output:


result:


Subtask #5:

score: 0
Runtime Error

Test #27:

score: 0
Runtime Error

input:

200000 200000
1861 126068
196536 190176
143836 3918
183352 173153
149804 74098
192775 110127
106287 1861
103451 149418
160851 35753
171988 21209
156996 161848
138469 165694
35753 140239
100453 199546
110790 120346
73215 149224
19332 138469
72674 21209
127832 142648
86197 174308
181332 15643
148621 1...

output:


result: