QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#711521#8333. Giftdgme-syz#WA 1ms3612kbC++232.0kb2024-11-05 11:42:162024-11-05 11:42:16

Judging History

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

  • [2024-11-05 11:42:16]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3612kb
  • [2024-11-05 11:42:16]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

vector<int> adj[100001];
int st[100001], ins[100001];
int fa[100001];
int ic[100001];
int cyc = 0;
void dfs(int u) {
    st[u] = ins[u] = 1;
    for (auto x : adj[u]) {
        fa[x] = u;
        if (!st[x]) dfs(x);
        else if (ins[x]) {
            for (int k = u;k != x;k = fa[k]) {
                ic[k] = 1, ++ cyc;
            }
            ic[x] = 1;
            ++ cyc;
        }
    }
    ins[u] = 0;
}
int d[100001];
int main() {
    cin.tie(0) -> sync_with_stdio(0);
    int N;cin >> N;
    for (int i = 2;i <= N;i ++ ) {
        int x, y;cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
        ++ d[x];
        ++ d[y];
    }
    dfs(1);
    for (int i = 1;i <= N;i ++ ) 
        if (!ic[i] && d[i] >= 5) {
            cout << "0";
            return 0;
        }
    for (int i = 1;i <= N;i ++ ) 
        if (ic[i] && d[i] > 5) {
            cout << "0";
            return 0;
        }
    vector<int> tmp;
    for (int i = 1;i <= N;i ++ ) 
        if (ic[i] && d[i] == 5) {
            tmp.push_back(i);
        }
    if (tmp.size() >= 3) {
        cout << "0";
        return 0;
    }
    if (tmp.size() == 2) {  
        int x = tmp[0], y = tmp[1];
        if (!(fa[x] == y || fa[y] == x)) {
            cout << "0";
            return 0;
        } 
    }
    if (!tmp.empty()) {
        int ans = 0;
        for (auto x : tmp) 
            -- d[x];
        for (int i = 1;i <= N;i ++ )
            if (d[i] <= 3) ans += (3 - tmp.size());
        cout << ans << '\n';
    } else {
        int ans = 0;
        for (int i = 1;i <= N;i ++ ) 
            if (!ic[i] && d[i] <= 3) {
                ans += cyc;
            }
        for (int i = 1;i <= N;i ++ )
            if (ic[i]) {
                if (d[i] <= 3) ans += cyc;
                else if (d[i] == 4) ans += cyc - 2;
                else assert(false);
            }
        cout << ans << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

input:

3
1 3
3 2
2 1

output:

12

result:

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