Submission #255075

#TimeUsernameProblemLanguageResultExecution timeMemory
255075davitmargGraph (BOI20_graph)C++17
100 / 100
198 ms28112 KiB
/* DavitMarg In a honky-tonk, Down in Mexico */ #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <iomanip> #include <bitset> #include <stack> #include <cassert> #include <iterator> #include <fstream> #define mod 1000000007ll #define LL long long #define LD long double #define MP make_pair #define PB push_back #define all(v) v.begin(), v.end() #define fastIO ios::sync_with_stdio(false); cin.tie(0) using namespace std; const int N = 200005; int n, m,used[N],d[N]; LD val, a[N]; vector<pair<int, int>> g[N]; vector<pair<int,LD>> vals; set<LD> x; void dfs(int v) { vals.push_back(MP(v, a[v])); used[v] = 1; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i].first; int val = g[v][i].second; if (used[to]) { if (d[v] != d[to]) { if (a[v] + a[to] != val) { cout << "NO" << endl; exit(0); } } else { LD X = (val - a[v] - a[to]) / 2.; if (d[v] == 0) x.insert(X); else x.insert(-X); } continue; } d[to] = d[v] ^ 1; a[to] = val - a[v]; dfs(to); } } int main() { fastIO; cin >> n >> m; while (m--) { int a, b, c; cin >> a >> b >> c; g[a].PB(MP(b, c)); g[b].PB(MP(a, c)); } for (int i = 1; i <= n; i++) { if (used[i]) continue; x.clear(); vals.clear(); dfs(i); if (x.size() > 1) { cout << "NO" << endl; exit(0); } LD X = 0; if (!x.empty()) X = *x.begin(); else { for (int j = 0; j < vals.size(); j++) if (!d[vals[j].first]) vals[j].second *= -1; sort(all(vals), [](pair<int, LD> a, pair<int, LD> b) { return a.second < b.second; }); X = vals[vals.size() / 2].second; } for (int j = 0; j < vals.size(); j++) if (!d[vals[j].first]) a[vals[j].first] += X; else a[vals[j].first] -= X; } cout << "YES" << endl; for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl; return 0; } /* */

Compilation message (stderr)

Graph.cpp: In function 'void dfs(int)':
Graph.cpp:45:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[v].size(); i++)
                     ~~^~~~~~~~~~~~~
Graph.cpp: In function 'int main()':
Graph.cpp:104:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int j = 0; j < vals.size(); j++)
                             ~~^~~~~~~~~~~~~
Graph.cpp:113:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < vals.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...