Submission #725066

#TimeUsernameProblemLanguageResultExecution timeMemory
725066_martynasGraph (BOI20_graph)C++11
100 / 100
251 ms20932 KiB
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define PB push_back #define all(a) (a).begin(), (a).end() using pii = pair<int, int>; using vi = vector<int>; // a*x+b struct LinearEq { double a, b; }; const int MXN = 1e5+5; const double EPS = 1e-6; int n, m; vector<pii> adj[MXN]; bool visited[MXN]; LinearEq leq[MXN]; double X[MXN]; bool bad = false; double x = -1.1e9; vi comp; void dfs(int u) { comp.PB(u); for(auto e : adj[u]) { int v = e.F; double w = e.S; if(visited[v]) { if(abs(leq[v].a)<EPS) { // v is known double xx = (w-(leq[u].b+leq[v].b))/leq[u].a; if(x > -1e9 && abs(x-xx) > EPS) bad = true; x = xx; continue; } if(abs(leq[u].a-leq[v].a)>EPS) { if(abs(w-(leq[u].b+leq[v].b)) > EPS) { bad = true; } } else { double xx = (w-(leq[u].b+leq[v].b))/(leq[u].a+leq[v].a); if(x > -1e9 && abs(x-xx) > EPS) bad = true; x = xx; } } else { leq[v].a = -leq[u].a; leq[v].b = w-leq[u].b; visited[v] = true; dfs(v); } } } int main() { cin >> n >> m; for(int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; adj[a].PB({b, c}); adj[b].PB({a, c}); } for(int i = 1; i <= n; i++) { sort(all(adj[i])); for(int j = 1; j < adj[i].size(); j++) { if(adj[i][j].F == adj[i][j-1].F && adj[i][j].S != adj[i][j-1].S) { cout << "NO\n"; return 0; } if(adj[i][j].F == i) { visited[i] = true; X[i] = 0.5*adj[i][j].S; leq[i].b = X[i]; } } } for(int i = 1; i <= n; i++) { if(visited[i]) continue; visited[i] = true; leq[i] = {1, 0}; x = -1.1e9; comp.clear(); dfs(i); if(bad) { cout << "NO\n"; return 0; } if(x < -1e9) { // if there was at least one known value all of the component would be known vector<double> vec; for(int u : comp) { vec.PB(-leq[u].a*leq[u].b); } sort(all(vec)); x = vec[vec.size()/2]; } for(int u : comp) { X[u] = leq[u].a*x+leq[u].b; } } cout << "YES\n"; for(int i = 1; i <= n; i++) { cout << X[i] << " \n"[i == n]; } return 0; }

Compilation message (stderr)

Graph.cpp: In function 'int main()':
Graph.cpp:72:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |         for(int j = 1; j < adj[i].size(); j++) {
      |                        ~~^~~~~~~~~~~~~~~
#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...