QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#653813#8333. GiftEliaukWA 1ms5720kbC++202.3kb2024-10-18 20:40:522024-10-18 20:40:52

Judging History

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

  • [2024-10-18 20:40:52]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5720kb
  • [2024-10-18 20:40:52]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
const ll INF = 1ll << 60;
typedef pair<int, int> PI;
const int N = 1e5 + 10;
int t;
int n;
vector<int> a[N];
map<int, int> mp;
bool judge[N];
int cnt[6];
vector<int> h;
bool dfs(int g, int fa)
{
    for (int i = 0; i < a[g].size(); i++)
    {
        if (a[g][i] == fa)
            continue;

        if (judge[a[g][i]])
        {
            h.push_back(g);
            return true;
        }
        else
        {
            judge[a[g][i]] = 1;
            bool vis = dfs(a[g][i], g);
            if (vis)
            {
                h.push_back(g);
                return true;
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int x, y;
        cin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
        mp[x]++;
        mp[y]++;
    }
    judge[1] = 1;
    dfs(1, -1);
    for (auto it : mp)
    {
        cnt[it.second]++;
    }
    if (cnt[5] == 2)
    {
        cout << n - cnt[5] - cnt[4] << endl;
    }
    else if (cnt[5] == 1)
    {
        cout << 2 * (n - cnt[5] - cnt[4]) << endl;
    }
    else
    {
        ll ans = 0;
        for (int i = 1; i < h.size(); i++)
        {
            if (mp[h[i]] < 4)
            {
                ans += (n - cnt[4]);
                if (mp[h[i - 1]] == 4)
                {
                    ans++;
                }
            }
            else
            {
                ans += (n - cnt[4] + 1);
                if (mp[h[i - 1]] == 4)
                {
                    ans++;
                }
            }
            if (n == h.size() - 1)
            {
                if (mp[h[i]] < 4)
                {
                    ans += (n - cnt[4]);
                    if (mp[h[0]] == 4)
                    {
                        ans++;
                    }
                }
                else
                {
                    ans += (n - cnt[4] + 1);
                    if (mp[h[0]] == 4)
                    {
                        ans++;
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5660kb

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

input:

3
1 3
3 2
2 1

output:

6

result:

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