QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#680814#8333. GiftDairuWA 1ms6392kbC++141.9kb2024-10-26 22:53:182024-10-26 22:53:19

Judging History

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

  • [2024-10-26 22:53:19]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:6392kb
  • [2024-10-26 22:53:18]
  • 提交

answer

#include <cstdio>
#include <vector>
#include <stack>
#include <iostream>

#define ll long long

using namespace std;

const int maxn = 1e5 + 7;

ll n;

vector<int> G[maxn], on_cycle;
bool vis[maxn];
stack<int> st;

bool dfs(int now, int prev)
{
    if (vis[now])
    {
        // printf("her %d\n", now);
        while (st.top() != now)
        {
            // printf("top %d\n", st.top());
            on_cycle.push_back(st.top());
            st.pop();
        }
        on_cycle.push_back(st.top());
        return 1;
    }
    st.push(now);
    vis[now] = 1;
    for (int i = 0; i < G[now].size(); ++i)
    {
        int to = G[now][i];
        if (to == prev) continue;
        // st.push(to);
        if (dfs(to, now)) return 1;
        // st.pop();
    }
    if (!st.empty())
        st.pop();
    return 0;
}


int main()
{
    scanf("%lld", &n);
    int x, y;
    for (int i = 0; i < n; ++i)
    {
        scanf("%d %d", &x, &y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1, 1);
    // for (int i = 0; i < on_cycle.size(); ++i)
    //     printf("%d ", on_cycle[i]);
    int pt = 0;
    for (int i = 1; i <= n; ++i)
        if (G[i].size() > 4) pt = i;
    int cnt = 0;
    if (pt)
    {
        // cout << pt <<endl;
        for (int i = 0; i < G[pt].size(); ++i)
        {
            int to = G[pt][i];
            bool on = 0;
            // printf("x%dx", to);
            for (int j = 0; j < on_cycle.size(); ++j)
            {
                
                if (to == on_cycle[j])
                {
                    on = 1;
                    break;
                }
            }
            if (on) ++cnt;
        }
        printf("%lld\n", (ll)cnt * (n - 1));
    }
    else
    {
        printf("%lld\n", n * (on_cycle.size() - 1));
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

input:

3
1 3
3 2
2 1

output:

6

result:

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