Submission #1303362

#TimeUsernameProblemLanguageResultExecution timeMemory
1303362ThanhsGraph (BOI20_graph)C++20
100 / 100
135 ms26644 KiB
#include <bits/stdc++.h> using namespace std; #define name "TENBAI" #define fi first #define se second #define int long long #define endl '\n' #define setmin(x, y) x = min((x), (y)) #define setmax(x, y) x = max((x), (y)) #define sqr(x) ((x) * (x)) #define all(x) x.begin(), x.end() mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count()); int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);} const int NM = 2e5 + 5; const double SUS = -69; pair<int, int> a[NM], sum; double force, siu[NM]; pair<double, double> ans; map<pair<int, int>, int> mp; int n, m; bool vis[NM]; vector<pair<int, int>> g[NM]; vector<int> cur; void dfs(int u) { cur.push_back(u); vis[u] = 1; mp[{a[u].fi, a[u].se}]++; for (auto t : g[u]) { int v = t.fi, w = t.se; if (!vis[v]) { a[v].fi = -a[u].fi, a[v].se = w - a[u].se; dfs(v); } else { if (a[u].fi + a[v].fi == 0) { if (a[u].se + a[v].se != w) { cout << "NO"; exit(0); } } else { double t = double(w - (a[u].se + a[v].se)) / (a[u].fi + a[v].fi); if (force == SUS) force = t; else if (force != t) { cout << "NO"; exit(0); } } } } } void solve() { force = -69; cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } for (int i = 1; i <= n; i++) if (!vis[i]) { cur.clear(); force = SUS; a[i] = {1, 0}; dfs(i); if (force != SUS) ans.se = force; else if (force == SUS) { ans.fi = 1e9; vector<array<int, 3>> v; for (auto t : mp) v.push_back({t.fi.fi, t.fi.se, t.se}); // sort(all(v), [&](auto x, auto y) {return -double(x[1]) / x[0] < -double(y[1]) / y[0];}); vector<double> TRY; for (auto x : v) TRY.push_back(-double(x[1]) / x[0]); for (auto t : TRY) { double sum = 0; for (int i : cur) sum += abs(t * a[i].fi + a[i].se); setmin(ans, make_pair(sum, t)); } } for (int i : cur) siu[i] = a[i].fi * ans.se + a[i].se; } cout << "YES\n"; for (int i = 1; i <= n; i++) cout << siu[i] << ' '; } signed main() { if (fopen("in.txt", "r")) { freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); } else if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int tc = 1; // cin >> tc; while (tc--) solve(); }

Compilation message (stderr)

Graph.cpp: In function 'int main()':
Graph.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Graph.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Graph.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |         freopen(name".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Graph.cpp:123:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |         freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...