Submission #1102014

# Submission time Handle Problem Language Result Execution time Memory
1102014 2024-10-17T09:50:20 Z NDT134 Graph (BOI20_graph) C++14
100 / 100
149 ms 11088 KB
#include<iostream>
#include<vector>
#include<queue>
#include<cmath>

using namespace std;
using pi = pair<int, int>;

int main() {
	int n, M; cin >> n >> M;
	vector<vector<pi>> g(n);
	for (int i = 0; i < M; i++)
	{
		int x, y, z; cin >> x >> y >> z; x--; y--;
		g[x].push_back({ y, z });
		g[y].push_back({ x, z });
	}
	
	vector<pi> v(n, { -1 , 0 });
	vector<double> r(n, 0.1);
	bool b = 1;
	for (int i = 0; i < n; i++)
	{
		if (!v[i].second)
		{
			double a = 0.1;
			v[i] = { 0, 1 };
			vector<pi> c(1,v[i]);
			vector<int> aa(1, i);
			queue<int> q; q.push(i);
			while (!q.empty() && a == 0.1)
			{
				int x = q.front(); q.pop();
				for (pi p : g[x])
				{
					int y = p.first;
					if (!v[y].second)
					{
						v[y] = { -v[x].first + p.second,-v[x].second };
						c.push_back(v[y]);
						aa.push_back(y);
						q.push(y);
					}
					else
					{
						if (v[y].second != v[x].second)
						{
							if (v[x].first + v[y].first != p.second)
							{
								cout << "NO" << endl;
								return 0;
							}
						}
						else
						{
							a = 0.5 * (v[y].first + v[x].first - p.second) * (-v[x].second);
							break;
						}
					}
				}
				
			}

			if (a != 0.1)
			{
				while (!q.empty())
				{
					q.pop();
				}
				q.push(i);
				r[i] = a;
				while (!q.empty())
				{
					int x = q.front(); q.pop();
					for (pi p : g[x])
					{
						int y = p.first;
						double rr = 1.0*p.second - r[x];
						if (r[y] != 0.1 && r[y] != rr)
						{
							cout << "NO" << endl;
							return 0;
						}
						if (r[y] == 0.1)
						{
							r[y] = rr;
							q.push(y);
							v[y] = { -2, -2 };
						}
					}
				}
			}
			else
			{
				/*
				int x = 0;
				for (pi p : c)
				{
					x += abs(p.first);
				}
				int mn = x, i = 0;
				for (int m = -2*n; m <= 2*n; m++)
				{
					x = 0;
					for (pi p : c)
					{
						x += abs(p.first + p.second * m);
					}
					if (x < mn)
					{
						mn = x;
						i = m;
					}
				}
				*/

				int mx = 2 * n, mn = -2 * n;
				while (mx - mn > 0)
				{

					int m = (mx + mn + 4*n) / 2 - 2*n;
					int x = 0;
					for (pi p : c)
					{
						x += abs(p.first + p.second * (m + 1));
					}
					int y = 0;
					for (pi p : c)
					{
						y += abs(p.first + p.second * m);
					}

					if (mx - mn == 1)
					{
						if (x >= y)
						{
							mx = mn;
						}
						else
						{
							mn = mx;
						}
					}
					else
					{
						if (x >= y)
						{
							mx = m;
						}
						else
						{
							mn = m + 1;
						}
					}
				}
				//*/


				for (int x : aa)
				{
					pi p = v[x];
					r[x] = p.first + p.second * mn;
				}
			}
		}
	}

	cout << "YES" << endl;
	for (int i = 0; i < n; i++)
	{
		cout << r[i] << " ";
	}
	cout << endl;
	return 0;
}

Compilation message

Graph.cpp: In function 'int main()':
Graph.cpp:21:7: warning: unused variable 'b' [-Wunused-variable]
   21 |  bool b = 1;
      |       ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB answer = YES
2 Correct 1 ms 336 KB answer = YES
3 Correct 1 ms 336 KB answer = YES
4 Correct 1 ms 504 KB answer = NO
5 Correct 1 ms 336 KB answer = YES
6 Correct 1 ms 336 KB answer = YES
7 Correct 1 ms 336 KB answer = YES
8 Correct 1 ms 336 KB answer = YES
9 Correct 1 ms 336 KB answer = NO
10 Correct 1 ms 336 KB answer = YES
11 Correct 1 ms 336 KB answer = YES
12 Correct 1 ms 336 KB answer = NO
13 Correct 1 ms 336 KB answer = YES
14 Correct 1 ms 336 KB answer = YES
15 Correct 1 ms 336 KB answer = YES
16 Correct 1 ms 336 KB answer = YES
17 Correct 1 ms 336 KB answer = YES
18 Correct 1 ms 336 KB answer = YES
19 Correct 1 ms 336 KB answer = YES
20 Correct 1 ms 336 KB answer = YES
21 Correct 1 ms 336 KB answer = YES
22 Correct 1 ms 336 KB answer = NO
23 Correct 1 ms 336 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB answer = YES
2 Correct 1 ms 336 KB answer = YES
3 Correct 1 ms 336 KB answer = YES
4 Correct 1 ms 504 KB answer = NO
5 Correct 1 ms 336 KB answer = YES
6 Correct 1 ms 336 KB answer = YES
7 Correct 1 ms 336 KB answer = YES
8 Correct 1 ms 336 KB answer = YES
9 Correct 1 ms 336 KB answer = NO
10 Correct 1 ms 336 KB answer = YES
11 Correct 1 ms 336 KB answer = YES
12 Correct 1 ms 336 KB answer = NO
13 Correct 1 ms 336 KB answer = YES
14 Correct 1 ms 336 KB answer = YES
15 Correct 1 ms 336 KB answer = YES
16 Correct 1 ms 336 KB answer = YES
17 Correct 1 ms 336 KB answer = YES
18 Correct 1 ms 336 KB answer = YES
19 Correct 1 ms 336 KB answer = YES
20 Correct 1 ms 336 KB answer = YES
21 Correct 1 ms 336 KB answer = YES
22 Correct 1 ms 336 KB answer = NO
23 Correct 1 ms 336 KB answer = NO
24 Correct 1 ms 336 KB answer = YES
25 Correct 1 ms 348 KB answer = YES
26 Correct 1 ms 336 KB answer = YES
27 Correct 1 ms 336 KB answer = YES
28 Correct 1 ms 336 KB answer = YES
29 Correct 1 ms 336 KB answer = YES
30 Correct 1 ms 504 KB answer = NO
31 Correct 1 ms 336 KB answer = YES
32 Correct 1 ms 336 KB answer = YES
33 Correct 1 ms 504 KB answer = YES
34 Correct 1 ms 336 KB answer = YES
35 Correct 1 ms 336 KB answer = YES
36 Correct 1 ms 336 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB answer = YES
2 Correct 1 ms 336 KB answer = YES
3 Correct 1 ms 336 KB answer = YES
4 Correct 1 ms 504 KB answer = NO
5 Correct 1 ms 336 KB answer = YES
6 Correct 1 ms 336 KB answer = YES
7 Correct 1 ms 336 KB answer = YES
8 Correct 1 ms 336 KB answer = YES
9 Correct 1 ms 336 KB answer = NO
10 Correct 1 ms 336 KB answer = YES
11 Correct 1 ms 336 KB answer = YES
12 Correct 1 ms 336 KB answer = NO
13 Correct 1 ms 336 KB answer = YES
14 Correct 1 ms 336 KB answer = YES
15 Correct 1 ms 336 KB answer = YES
16 Correct 1 ms 336 KB answer = YES
17 Correct 1 ms 336 KB answer = YES
18 Correct 1 ms 336 KB answer = YES
19 Correct 1 ms 336 KB answer = YES
20 Correct 1 ms 336 KB answer = YES
21 Correct 1 ms 336 KB answer = YES
22 Correct 1 ms 336 KB answer = NO
23 Correct 1 ms 336 KB answer = NO
24 Correct 1 ms 336 KB answer = YES
25 Correct 1 ms 348 KB answer = YES
26 Correct 1 ms 336 KB answer = YES
27 Correct 1 ms 336 KB answer = YES
28 Correct 1 ms 336 KB answer = YES
29 Correct 1 ms 336 KB answer = YES
30 Correct 1 ms 504 KB answer = NO
31 Correct 1 ms 336 KB answer = YES
32 Correct 1 ms 336 KB answer = YES
33 Correct 1 ms 504 KB answer = YES
34 Correct 1 ms 336 KB answer = YES
35 Correct 1 ms 336 KB answer = YES
36 Correct 1 ms 336 KB answer = YES
37 Correct 1 ms 336 KB answer = YES
38 Correct 1 ms 336 KB answer = YES
39 Correct 1 ms 336 KB answer = YES
40 Correct 1 ms 336 KB answer = YES
41 Correct 1 ms 336 KB answer = NO
42 Correct 1 ms 336 KB answer = YES
43 Correct 2 ms 520 KB answer = YES
44 Correct 1 ms 336 KB answer = YES
45 Correct 1 ms 336 KB answer = YES
46 Correct 1 ms 336 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 348 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB answer = YES
2 Correct 1 ms 336 KB answer = YES
3 Correct 1 ms 336 KB answer = YES
4 Correct 1 ms 504 KB answer = NO
5 Correct 1 ms 336 KB answer = YES
6 Correct 1 ms 336 KB answer = YES
7 Correct 1 ms 336 KB answer = YES
8 Correct 1 ms 336 KB answer = YES
9 Correct 1 ms 336 KB answer = NO
10 Correct 1 ms 336 KB answer = YES
11 Correct 1 ms 336 KB answer = YES
12 Correct 1 ms 336 KB answer = NO
13 Correct 1 ms 336 KB answer = YES
14 Correct 1 ms 336 KB answer = YES
15 Correct 1 ms 336 KB answer = YES
16 Correct 1 ms 336 KB answer = YES
17 Correct 1 ms 336 KB answer = YES
18 Correct 1 ms 336 KB answer = YES
19 Correct 1 ms 336 KB answer = YES
20 Correct 1 ms 336 KB answer = YES
21 Correct 1 ms 336 KB answer = YES
22 Correct 1 ms 336 KB answer = NO
23 Correct 1 ms 336 KB answer = NO
24 Correct 1 ms 336 KB answer = YES
25 Correct 1 ms 348 KB answer = YES
26 Correct 1 ms 336 KB answer = YES
27 Correct 1 ms 336 KB answer = YES
28 Correct 1 ms 336 KB answer = YES
29 Correct 1 ms 336 KB answer = YES
30 Correct 1 ms 504 KB answer = NO
31 Correct 1 ms 336 KB answer = YES
32 Correct 1 ms 336 KB answer = YES
33 Correct 1 ms 504 KB answer = YES
34 Correct 1 ms 336 KB answer = YES
35 Correct 1 ms 336 KB answer = YES
36 Correct 1 ms 336 KB answer = YES
37 Correct 1 ms 336 KB answer = YES
38 Correct 1 ms 336 KB answer = YES
39 Correct 1 ms 336 KB answer = YES
40 Correct 1 ms 336 KB answer = YES
41 Correct 1 ms 336 KB answer = NO
42 Correct 1 ms 336 KB answer = YES
43 Correct 2 ms 520 KB answer = YES
44 Correct 1 ms 336 KB answer = YES
45 Correct 1 ms 336 KB answer = YES
46 Correct 1 ms 336 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 348 KB answer = YES
49 Correct 10 ms 1360 KB answer = YES
50 Correct 8 ms 1188 KB answer = YES
51 Correct 8 ms 1452 KB answer = YES
52 Correct 5 ms 1104 KB answer = NO
53 Correct 2 ms 336 KB answer = YES
54 Correct 2 ms 592 KB answer = YES
55 Correct 4 ms 848 KB answer = YES
56 Correct 8 ms 1360 KB answer = YES
57 Correct 8 ms 1104 KB answer = YES
58 Correct 8 ms 1104 KB answer = YES
59 Correct 8 ms 1104 KB answer = YES
60 Correct 9 ms 1104 KB answer = YES
61 Correct 4 ms 848 KB answer = YES
62 Correct 88 ms 5196 KB answer = NO
63 Correct 82 ms 5192 KB answer = YES
64 Correct 82 ms 5192 KB answer = NO
65 Correct 84 ms 5192 KB answer = YES
66 Correct 2 ms 592 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB answer = YES
2 Correct 1 ms 336 KB answer = YES
3 Correct 1 ms 336 KB answer = YES
4 Correct 1 ms 504 KB answer = NO
5 Correct 1 ms 336 KB answer = YES
6 Correct 1 ms 336 KB answer = YES
7 Correct 1 ms 336 KB answer = YES
8 Correct 1 ms 336 KB answer = YES
9 Correct 1 ms 336 KB answer = NO
10 Correct 1 ms 336 KB answer = YES
11 Correct 1 ms 336 KB answer = YES
12 Correct 1 ms 336 KB answer = NO
13 Correct 1 ms 336 KB answer = YES
14 Correct 1 ms 336 KB answer = YES
15 Correct 1 ms 336 KB answer = YES
16 Correct 1 ms 336 KB answer = YES
17 Correct 1 ms 336 KB answer = YES
18 Correct 1 ms 336 KB answer = YES
19 Correct 1 ms 336 KB answer = YES
20 Correct 1 ms 336 KB answer = YES
21 Correct 1 ms 336 KB answer = YES
22 Correct 1 ms 336 KB answer = NO
23 Correct 1 ms 336 KB answer = NO
24 Correct 1 ms 336 KB answer = YES
25 Correct 1 ms 348 KB answer = YES
26 Correct 1 ms 336 KB answer = YES
27 Correct 1 ms 336 KB answer = YES
28 Correct 1 ms 336 KB answer = YES
29 Correct 1 ms 336 KB answer = YES
30 Correct 1 ms 504 KB answer = NO
31 Correct 1 ms 336 KB answer = YES
32 Correct 1 ms 336 KB answer = YES
33 Correct 1 ms 504 KB answer = YES
34 Correct 1 ms 336 KB answer = YES
35 Correct 1 ms 336 KB answer = YES
36 Correct 1 ms 336 KB answer = YES
37 Correct 1 ms 336 KB answer = YES
38 Correct 1 ms 336 KB answer = YES
39 Correct 1 ms 336 KB answer = YES
40 Correct 1 ms 336 KB answer = YES
41 Correct 1 ms 336 KB answer = NO
42 Correct 1 ms 336 KB answer = YES
43 Correct 2 ms 520 KB answer = YES
44 Correct 1 ms 336 KB answer = YES
45 Correct 1 ms 336 KB answer = YES
46 Correct 1 ms 336 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 348 KB answer = YES
49 Correct 10 ms 1360 KB answer = YES
50 Correct 8 ms 1188 KB answer = YES
51 Correct 8 ms 1452 KB answer = YES
52 Correct 5 ms 1104 KB answer = NO
53 Correct 2 ms 336 KB answer = YES
54 Correct 2 ms 592 KB answer = YES
55 Correct 4 ms 848 KB answer = YES
56 Correct 8 ms 1360 KB answer = YES
57 Correct 8 ms 1104 KB answer = YES
58 Correct 8 ms 1104 KB answer = YES
59 Correct 8 ms 1104 KB answer = YES
60 Correct 9 ms 1104 KB answer = YES
61 Correct 4 ms 848 KB answer = YES
62 Correct 88 ms 5196 KB answer = NO
63 Correct 82 ms 5192 KB answer = YES
64 Correct 82 ms 5192 KB answer = NO
65 Correct 84 ms 5192 KB answer = YES
66 Correct 2 ms 592 KB answer = YES
67 Correct 80 ms 9056 KB answer = YES
68 Correct 78 ms 9080 KB answer = YES
69 Correct 76 ms 9028 KB answer = YES
70 Correct 124 ms 10568 KB answer = YES
71 Correct 80 ms 8972 KB answer = YES
72 Correct 94 ms 9604 KB answer = YES
73 Correct 92 ms 9404 KB answer = YES
74 Correct 55 ms 5704 KB answer = YES
75 Correct 38 ms 5804 KB answer = NO
76 Correct 11 ms 1532 KB answer = YES
77 Correct 21 ms 2648 KB answer = YES
78 Correct 39 ms 4300 KB answer = YES
79 Correct 88 ms 8384 KB answer = YES
80 Correct 60 ms 5868 KB answer = YES
81 Correct 56 ms 7752 KB answer = NO
82 Correct 92 ms 8772 KB answer = YES
83 Correct 100 ms 9024 KB answer = YES
84 Correct 92 ms 9032 KB answer = YES
85 Correct 83 ms 9024 KB answer = YES
86 Correct 81 ms 9156 KB answer = YES
87 Correct 59 ms 7820 KB answer = NO
88 Correct 93 ms 8152 KB answer = YES
89 Correct 89 ms 7752 KB answer = YES
90 Correct 86 ms 7760 KB answer = YES
91 Correct 101 ms 7764 KB answer = YES
92 Correct 45 ms 4556 KB answer = YES
93 Correct 42 ms 4548 KB answer = YES
94 Correct 56 ms 9028 KB answer = NO
95 Correct 54 ms 7236 KB answer = NO
96 Correct 149 ms 11088 KB answer = YES
97 Correct 50 ms 9032 KB answer = NO