QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#644962#8333. GiftSmallBeeWA 1ms6312kbC++171.9kb2024-10-16 16:13:132024-10-16 16:13:14

Judging History

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

  • [2024-10-16 16:13:14]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:6312kb
  • [2024-10-16 16:13:13]
  • 提交

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;
                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: 5888kb

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

input:

3
1 3
3 2
2 1

output:

12

result:

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