QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#710045 | #8333. Gift | xggx | TL | 1ms | 4420kb | C++20 | 2.2kb | 2024-11-04 18:15:27 | 2024-11-04 18:15:27 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int n;
vector<vector<int>> adj;
int fa[N], deg[N];
vector<int> path, pre;
int get(int x) {
if (x == fa[x]) {
return x;
}
return fa[x] = get(fa[x]);
}
void dfs(int x, int y) {
if (x == y) {
int cur = x;
while (cur != -1) {
path.push_back(cur);
cur = pre[cur];
}
return;
}
for (auto v : adj[x]) {
if (pre[v] == 0) {
pre[v] = x;
dfs(v, y);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
cin >> n;
adj.resize(n);
pre.resize(n);
iota(fa, fa + N, 0);
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
x--, y--;
deg[x]++, deg[y]++;
if (get(x) != get(y)) {
adj[x].push_back(y);
adj[y].push_back(x);
fa[get(x)] = get(y);
} else pre[x] = -1, dfs(x, y);
}
int d4 = 0, dg4 = 0;
for (int i = 0; i < n; i++) {
if (deg[i] == 4) d4++;
else if (deg[i] > 4) dg4++;
}
ll ans = 0;
int len = path.size();
// cout << len;
for (int i = 0; i < (int)path.size(); i++) {
int u = path[i], v = path[(i + 1) % len];
if (deg[u] == 4) d4--;
else if (deg[u] > 4) dg4--;
if (deg[v] == 4) d4--;
else if (deg[v] > 4) dg4--;
deg[u]--, deg[v]--;
if (deg[u] == 4) d4++;
else if (deg[u] > 4) dg4++;
if (deg[v] == 4) d4++;
else if (deg[v] > 4) dg4++;
if (dg4 == 0) ans += n - d4;
if (deg[u] == 4) d4--;
else if (deg[u] > 4) dg4--;
if (deg[v] == 4) d4--;
else if (deg[v] > 4) dg4--;
deg[u]++, deg[v]++;
if (deg[u] == 4) d4++;
else if (deg[u] > 4) dg4++;
if (deg[v] == 4) d4++;
else if (deg[v] > 4) dg4++;
}
cout << ans;
return 0;
}
/*
6
1 2
1 3
1 4
1 5
1 6
2 3
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 4048kb
input:
6 1 2 1 3 1 4 1 5 1 6 2 3
output:
10
result:
ok 1 number(s): "10"
Test #2:
score: 0
Accepted
time: 1ms
memory: 4048kb
input:
3 1 3 3 2 2 1
output:
9
result:
ok 1 number(s): "9"
Test #3:
score: 0
Accepted
time: 1ms
memory: 4420kb
input:
2332 1648 909 1676 2122 1644 1981 1106 1131 1785 239 223 618 335 1662 424 1775 889 1684 1589 52 1406 1747 1600 302 790 2056 1742 464 1706 541 1145 779 2316 833 1645 1439 859 438 1337 136 746 1565 436 1730 2079 2145 1583 1940 917 1549 1863 507 1266 367 1890 2230 13 2113 492 2109 120 1122 815 1111 134...
output:
5438224
result:
ok 1 number(s): "5438224"
Test #4:
score: -100
Time Limit Exceeded
input:
100000 46198 33056 80285 88339 88963 925 43203 66233 13618 35880 19864 76475 90021 73072 3202 63653 41571 83067 22067 98768 10753 16653 32856 85797 3483 2064 46659 9486 23290 82179 97966 23617 81566 7334 81774 76138 10959 75816 93471 12058 97260 66262 85541 78476 67864 87220 8607 52245 38957 67603 7...