QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#645137#8333. GiftSmallBeeWA 1ms5928kbC++142.2kb2024-10-16 17:02:442024-10-16 17:02:50

Judging History

你现在查看的是最新测评结果

  • [2024-10-16 17:02:50]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5928kb
  • [2024-10-16 17:02:44]
  • 提交

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: 1ms
memory: 5928kb

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: 5808kb

input:

3
1 3
3 2
2 1

output:

3 1
1 2
2 3
9

result:

wrong answer 1st numbers differ - expected: '9', found: '3'