QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#666140 | #8333. Gift | zzisjtu# | WA | 0ms | 3848kb | C++23 | 1.4kb | 2024-10-22 16:47:49 | 2024-10-22 16:47:53 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n;
cin >> n;
vector<vector<int>> e(n + 1);
vector<int>in(n + 1), u(n + 1), v(n + 1);
for(int i = 1; i <= n; i++) {
cin >> u[i] >> v[i];
in[u[i]]++;
in[v[i]]++;
e[u[i]].push_back(v[i]);
e[v[i]].push_back(u[i]);
}
auto inn = in;
auto toposort = [&]() {
queue<int> q;
for(int i = 1; i <= n; i++) {
if(in[i] == 1)q.push(i);
}
while(q.size()) {
int u = q.front();
q.pop();
for(int v: e[u]) {
if(in[v] > 1) {//in[v] >= 2 的点即为环上的点
in[v]--;
if(in[v] == 1)q.push(v);
}
}
}
};
vector<int> cnt(n + 1, 0);
for(int i = 1; i <= n; i++) {
cnt[inn[i]] += 1;
}
toposort();
int ans = 0;
vector<array<int, 2>> res;
for(int i = 1; i <= n; i++) {
if(in[u[i]] >= 2 && in[v[i]] >= 2) { //环边
res.push_back({u[i], v[i]});
}
}
for(auto [u, v]: res) {
cnt[inn[u]]--;
cnt[inn[v]]--;
cnt[inn[u] - 1]++;
cnt[inn[v] - 1]++;
if(cnt[5]) {
cnt[inn[u]]++;
cnt[inn[v]]++;
cnt[inn[u] - 1]--;
cnt[inn[v] - 1]--;
continue;
}
ans += cnt[1] + cnt[2] + cnt[3];
cnt[inn[u]]++;
cnt[inn[v]]++;
cnt[inn[u] - 1]--;
cnt[inn[v] - 1]--;
}
cout << ans << endl;
}
#undef int
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3624kb
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: -100
Wrong Answer
time: 0ms
memory: 3848kb
input:
3 1 3 3 2 2 1
output:
0
result:
wrong answer 1st numbers differ - expected: '9', found: '0'