Submission #1102007

#TimeUsernameProblemLanguageResultExecution timeMemory
1102007NDT134Graph (BOI20_graph)C++14
58 / 100
1032 ms9024 KiB
#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; } } for (int x : aa) { pi p = v[x]; r[x] = p.first + p.second * i; } } } } cout << "YES" << endl; for (int i = 0; i < n; i++) { cout << r[i] << " "; } cout << endl; return 0; }

Compilation message (stderr)

Graph.cpp: In function 'int main()':
Graph.cpp:21:7: warning: unused variable 'b' [-Wunused-variable]
   21 |  bool b = 1;
      |       ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...