版本比较
题解:USACO First Contest S2:Mooclear Reactor
正文
增加 12 行删除 12 行
126 行未改动全部展开1271281291301311311321331341351361371381391401401411421431441451461461471481491501511521531531541551561574 行未改动1621631641651661661671681691702 行未改动17317417517617717717817918018115 行未改动1971981992002012012022032042054 行未改动2102112122132142142152162172185 行未改动2242252262272282282292302312325 行未改动2382392402412422422432442442452462472472482492502516 行未改动126 行未改动全部展开typedef long long ll;
const ll inf = (1LL << 60);
const int N = 1e6 + 10; // 开大点,防止 RE const int N = 1e6 + 10; // 开大点,防止 RE
struct Edge { ll to, w;};int T, n, m;ll l[N], r[N];vector <Edge> edge[N];ll a[N];bool ans = true; // ans 记录有无解 bool ans = true; // ans 记录有无解ll col[N], f[N];bool b[N];vector <ll> path, tr, num;ll s1, s2;
inline void dfs1(int x) { // 在已知一个 a[i] 时推出连通块中的其他值 inline void dfs1(int x) { // 在已知一个 a[i] 时推出连通块中的其他值 for (auto y : edge[x]) { if (a[y.to] < -inf) a[y.to] = y.w - a[x], dfs1(y.to); else if (a[y.to] + a[x] != y.w) ans = false; }}
int dfs2(int x) { // 判二分图,找奇环 int dfs2(int x) { // 判二分图,找奇环 path.push_back(x); for (auto y : edge[x]) { if (!col[y.to]) { col[y.to] = col[x] ^ 3;4 行未改动 path.pop_back(); return 0;}
inline void dfs3(int x, int e) { // 解奇环里的方程(e 表示 p[e] 为目标点) inline void dfs3(int x, int e) { // 解奇环里的方程(e 表示 p[e] 为目标点) for (auto y : edge[x]) if (y.to == path[e]) { s1 += y.w; if (!e) return;2 行未改动 return; }}
inline void dfs4(int x) { // 加连通块里点,除初始点 inline void dfs4(int x) { // 加连通块里点,除初始点 b[x] = true; for (auto y : edge[x]) if (!b[y.to]) tr.push_back(y.to), dfs4(y.to);}15 行未改动 } else edge[x].push_back({y, z}), edge[y].push_back({x, z}); } for (int i = 1 ; i <= n ; i++) // Part 2 if (a[i] > -inf) dfs1(i); int res = 0; // res 记答案 int res = 0; // res 记答案 for (int i = 1 ; i <= n ; i++) if (a[i] < -inf) { col[i] = 1; path.clear(), tr.clear(), num.clear(); int x = dfs2(i);4 行未改动 int v = path.back(); if (v == x) break; path.pop_back(); } assert(path.size() & 1); // 现在 path 里的点为奇环上的点 assert(path.size() & 1); // 现在 path 里的点为奇环上的点 s1 = s2 = 0; // s1 是方程右式的和,s2 是环上的 a 之和减 a[path[0]] dfs3(path.front(), 1); if (s1 & 1) ans = false; a[path.front()] = s1 / 2 - s2;5 行未改动 for (auto x : tr) { if (col[x] == 1) l[x] -= a[x], r[x] -= a[x]; else l[x] = a[x] - l[x], r[x] = a[x] - r[x], swap(l[x], r[x]); } num.clear(); // begin num.clear(); // begin tr.push_back(i); for (auto x : tr) num.push_back(l[x]), num.push_back(r[x] + 1); sort(num.begin(), num.end()); num.erase(unique(num.begin(), num.end()), num.end());5 行未改动 } for (int j = 1 ; j < (int) num.size() ; j++) f[j] += f[j - 1]; ll w = 0; for (int j = 0 ; j < (int) num.size() ; j++) w = max(w, f[j]); res += w; // end 这段求 Part 4 的答案 res += w; // end 这段求 Part 4 的答案 for (auto x : tr) a[x] = -inf; // 还原 a[i],不然判 Part 1、2、3 的合法 a[i] 可能会把 Part 4 判进去 // 还原 a[i],不然判 Part 1、2、3 的合法 a[i] 可能会把 Part 4 判进去 } } for (int i = 1 ; i <= n ; i++) // 判 Part 1、2、3 的合法 a[i] 个数 for (int i = 1 ; i <= n ; i++) // 判 Part 1、2、3 的合法 a[i] 个数 res += l[i] <= a[i] && a[i] <= r[i]; if (!ans) { puts("-1"); continue;6 行未改动