QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#645146 | #8333. Gift | SmallBee | WA | 2ms | 6024kb | C++14 | 2.2kb | 2024-10-16 17:04:06 | 2024-10-16 17:04:07 |
Judging History
answer
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
typedef pair<int, int> PII;
const int N = 100010;
int n;
vector<int> h[N];
int d[N];
void topsort()
{
queue<int> q;
for (int i = 1; i <= n; i++)
{
d[i] = h[i].size();
if (d[i] == 1) q.push(i);
}
while (q.size())
{
auto t = q.front();
q.pop();
for (int i = 0; i < h[t].size(); i++)
{
int j = h[t][i];
d[j]--;
if (d[j] == 1) q.push(j);
}
}
}
signed main()
{
ios ::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
int u, v;
cin >> u >> v;
h[u].push_back(v), h[v].push_back(u);
// cout << u << ' ' << v << endl;
}
topsort();
int c = 0, cc = 0, c2 = 0;
for (int i = 1; i <= n; i++)
{
if (h[i].size() == 4) c++;
if (h[i].size() == 5) cc++, c2 = i;
}
if (cc == 2) cout << n - c - 2 << endl;
else if (cc == 1)
{
int cnt = 0;
for (int i = 0; i < h[c2].size(); i++)
{
int j = h[c2][i];
if (d[j] == 1) continue;
// cout << j << endl;
if (h[j].size() == 4) cnt++;
}
// cout << h[3].size() << endl;
// cout << n << ' ' << c << ' ' << cnt << endl;
cout << (n - 1 - c) * 2 + cnt << endl;
}
else
{
int u = 0, cnt = 0;
for (int i = 1; i <= n; i++)
if (d[i] > 1)
{
u = i;
cnt++;
}
bool flag = false;
int res = 0, fa = 0, st = u;
for (int t = 1; t < cnt; t++)
{
for (int i = 0; i < h[u].size(); i++)
{
int v = h[u][i];
if (v == fa || d[v] == 1) continue;
// cout << u << ' ' << v << endl;
int c1 = (h[u].size() == 4);
if (h[v].size() == 4) res += n - c + 1 + c1;
else res += n - c + c1;
fa = u, u = v;
}
}
cout << res << endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5864kb
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: 5812kb
input:
3 1 3 3 2 2 1
output:
9
result:
ok 1 number(s): "9"
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 6024kb
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:
8120024
result:
wrong answer 1st numbers differ - expected: '5438224', found: '8120024'