Submission #486034

#TimeUsernameProblemLanguageResultExecution timeMemory
486034chungdinhGraph (BOI20_graph)C++17
100 / 100
242 ms23480 KiB
#include <iostream> #include <vector> #include <cstring> #include <queue> #include <algorithm> using namespace std; #define ii pair<int, int> #define MP make_pair const int N = 5e5 + 5; const int iINF = 1e9; const double dINF = 10000; int n, m; vector<ii> g[N]; int vsd[N]; ii z[N]; double res[N]; double subsub(const ii& a, const ii& b) { if (a.first == b.first) { if (a.second == b.second) return dINF; else return -dINF; } return (double)(b.second - a.second) / (a.first - b.first); } ii transfer(ii a, int c) {return {-a.first, c - a.second};} bool sub(int ux) { queue<int> q; q.push(ux); z[ux] = {1, 0}; vsd[ux] = 0; res[ux] = -dINF; vector<int> lst; while (q.size()) { int u = q.front(); q.pop(); { ii tmp = z[u]; if (tmp.first < 0) tmp = {-tmp.first, -tmp.second}; lst.push_back(-tmp.second); } for (ii a : g[u]) { int v = a.first, c = a.second; if (vsd[v] < 0) { z[v] = transfer(z[u], c); vsd[v] = ux; q.push(v); } else { double tmp = subsub(transfer(z[u], c), z[v]); //cout << u << " " << v << " " << tmp << endl; if (tmp == dINF) continue; else if (tmp == -dINF) return false; else { if (res[ux] == -dINF || res[ux] == tmp) res[ux] = tmp; else return false; } } } } if (res[ux] == -dINF) { sort(lst.begin(), lst.end()); res[ux] = lst[lst.size() / 2]; } //cout << ux << " " << res[ux] << endl; return true; } int main() { #ifdef CHUNGDINH freopen("main.inp", "r", stdin); #endif // CHUNGDINH cin >> n >> m; for (int i = 0; i < m; i++) { int u, v, c; cin >> u >> v >> c; g[u].push_back(MP(v, c)); g[v].push_back(MP(u, c)); } for (int i = 1; i <= n; i++) vsd[i] = -1; for (int i = 1; i <= n; i++) if (vsd[i] < 0) { if (!sub(i)) { cout << "NO"; return 0; } } //for (int i = 1; i <= n; i++) cout << z[i].first << " " << z[i].second << endl; cout << "YES\n"; for (int i = 1; i <= n; i++) { if (!vsd[i]) {} else res[i] = res[vsd[i]] * z[i].first + z[i].second; cout << res[i] << " "; } }
#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...