QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#734278 | #8333. Gift | Unalome | WA | 1ms | 3844kb | C++20 | 3.1kb | 2024-11-11 08:26:56 | 2024-11-11 08:26:57 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
// #define pb emplace_back
// #define PII pair<int, int>
using namespace std;
const int maxn = 1e5 + 5;
int n;
vector<int> edge[maxn], circle;
int deg[maxn], tmp[maxn];
int ans = 0;
bool vis[maxn];
void toposort()
{
queue<int> q;
for (int i = 1; i <= n; i++)
vis[i] = 0;
for (int i = 1; i <= n; i++)
if (tmp[i] == 1)
q.push(i), vis[i] = true;
while (!q.empty())
{
int u = q.front();
q.pop();
for (auto v : edge[u])
if (--tmp[v] <= 1 && !vis[v])
q.push(v), vis[v] = true;
}
}
int st;
void findcircle(int u, int fa)
{
if (u == st)
return;
for (auto v : edge[u])
{
if (v == fa)
continue;
if (tmp[v] > 1)
{
circle.emplace_back(v);
findcircle(v, u);
}
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
int u, v;
cin >> u >> v;
edge[u].emplace_back(v);
edge[v].emplace_back(u);
deg[u]++, deg[v]++;
tmp[u]++, tmp[v]++; // 临时度
}
toposort();
for (st = 1; st <= n; st++)
{
if (tmp[st] > 1)
{
circle.emplace_back(st);
findcircle(st, 0);
break;
}
}
int below4 = 0;
int deg51 = 0, deg52 = 0, cnt5 = 0;
for (int i = 1; i <= n; i++)
{
below4 += (deg[i] < 4) ? 1 : 0;
if (deg[i] == 5)
{
if (cnt5 == 1)
{
deg52 = i;
cnt5++;
}
else
{
deg51 = i;
cnt5++;
}
}
}
if (cnt5 == 0)
{
for (int i = 0; i < circle.size(); i++)
{
if (deg[circle[i]] == 4)
{
int pos = i;
if (deg[circle[(pos - 1 == -1) ? circle.size() - 1 : pos - 1]] == 4)
ans += below4 + 2;
else
ans += below4 + 1;
}
else
{
int pos = i;
if (deg[circle[(pos - 1 == -1) ? circle.size() - 1 : pos - 1]] == 4)
ans += below4 + 1;
else
ans += below4;
}
}
}
else if (cnt5 == 1)
{
int pos;
int len = circle.size() - 1;
for (int i = 0; i < circle.size(); i++)
{
if (deg51 == circle[i])
{
pos = i;
break;
}
}
if (deg[circle[(pos - 1 == -1) ? len : pos - 1]] == 4)
ans += below4 + 1;
else
ans += below4;
if (deg[circle[(pos + 1 == len + 1) ? 0 : pos + 1]] == 4)
ans += below4 + 1;
else
ans += below4;
}
else
{
ans += below4;
}
cout << ans << "\n";
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3616kb
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: 1ms
memory: 3844kb
input:
3 1 3 3 2 2 1
output:
3
result:
wrong answer 1st numbers differ - expected: '9', found: '3'