QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#644960 | #8333. Gift | SmallBee | WA | 2ms | 6452kb | C++17 | 1.9kb | 2024-10-16 16:12:43 | 2024-10-16 16:12:43 |
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);
}
topsort();
int c = 0, cc = 0;
for (int i = 1; i <= n; i++)
{
if (h[i].size() == 4) c++;
if (h[i].size() == 5) cc = i;
}
if (cc)
{
int cnt = 0;
for (int i = 0; i < h[cc].size(); i++)
{
int j = h[cc][i];
if (d[j] == 1) continue;
if (h[j].size() == 4) cnt++;
}
cout << (n - 1) * 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;
if (h[v].size() == 4) res += n - c + 2;
else res += n - c + 1;
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: 1ms
memory: 6452kb
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: 2ms
memory: 6120kb
input:
3 1 3 3 2 2 1
output:
3 1 1 2 2 3 12
result:
wrong answer 1st numbers differ - expected: '9', found: '3'