QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#613574#852. Jellyfishgambit#WA 407ms4036kbC++172.3kb2024-10-05 14:15:232024-10-05 14:17:10

Judging History

This is the latest submission verdict.

  • [2024-10-05 14:17:10]
  • Judged
  • Verdict: WA
  • Time: 407ms
  • Memory: 4036kb
  • [2024-10-05 14:15:23]
  • Submitted

answer

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;

int T;
const int NN = 1e5+5;
int N;

bool vis[NN];

int DFS(vector<vector<int>> &EG, int prv, int cur){
    vis[cur] = true;
    for(auto nxt:EG[cur]){
        if(nxt==prv)continue;
        if(vis[nxt]){
            return nxt;
        }
        int ret = DFS(EG, cur, nxt);
        if(ret!=-1)return ret;
    }
    return -1;
}
vector<int> BFS(vector<vector<int>> &EG, int start){
    vector<int> rst;
    queue<pair<int, int>> q;
    vector<int> his(N+1);
    q.push({0, start});
    vis[start] = true;
    his[start] = start;
    while(!q.empty()){
        auto f = q.front();q.pop();
        int prv = f.first, cur = f.second;
        for(auto nxt:EG[cur]){
            if(nxt==prv)continue;
            if(vis[nxt]){
                while(nxt!=start){
                    rst.push_back(nxt);
                    nxt = his[nxt];
                }
                rst.push_back(start);
                reverse(all(rst));
                while(cur!=start){
                    rst.push_back(cur);
                    cur = his[cur];
                }
                return rst;
            }
            vis[nxt] = true;
            his[nxt] = cur;
            q.push({cur, nxt});
        }
    }
}


void sol(){
    vector<vector<int>> EG;
    memset(vis, 0, sizeof(vis));
    cin>>N;
    EG.assign(N+1, vector<int>());
    
    int ans = 0;
    for(int i=0;i<N;i++){
        int u, v;cin>>u>>v;
        EG[u].push_back(v);
        EG[v].push_back(u);
    }
    for(int i=1;i<=N;i++){
        if(EG[i].size()==1)ans++;
    }

    int start = DFS(EG, 0, 1);
    memset(vis, 0, sizeof(vis));
    auto cycle = BFS(EG, start);
    if(cycle.size()==N){
        cout<<3<<'\n';
        return;
    }
    // cout<<start<<'\n';
    // for(auto c:cycle)cout<<c<<' ';cout<<'\n';
    int mx = 0;
    for(int i=0;i<cycle.size();i++){
        if(EG[i].size()==2){
            mx = max(mx, 1);
            if(EG[(i+1)/EG[i].size()].size()==2)mx = max(mx, 2);
        }
    }

    cout<<ans+mx<<'\n';

}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int T;
    cin>>T;
    for(int i=0;i<T;i++)sol();
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
6
1 2
2 3
3 4
4 1
2 5
2 6
4
1 2
2 3
3 4
4 1

output:

4
3

result:

ok 2 number(s): "4 3"

Test #2:

score: -100
Wrong Answer
time: 407ms
memory: 4036kb

input:

85665
6
3 2
4 1
4 6
2 1
2 6
5 1
7
6 2
6 3
5 1
1 2
2 4
4 7
3 7
7
6 1
6 7
1 4
1 3
7 5
5 3
4 2
7
6 2
7 4
7 5
3 1
3 4
2 5
1 4
7
7 2
2 6
5 4
5 6
5 1
3 1
4 6
7
3 5
3 1
3 7
3 2
5 1
5 4
4 6
7
4 5
4 1
3 6
3 7
6 7
6 1
2 1
7
5 3
7 3
1 4
6 2
6 3
2 3
4 3
7
2 3
2 6
2 4
7 5
3 5
5 1
1 4
7
3 4
3 7
5 6
2 7
4 6
6 7
6 ...

output:

2
3
2
3
4
5
2
4
4
4
4
4
3
3
3
2
4
4
4
3
4
4
4
3
3
3
3
9
3
5
3
4
7
4
99
4
3
2
6
3
4
3
4
4
5
5
4
2
4
3
5
3
3
4
97
2
4
4
5
4
2
4
2
4
4
1
4
3
3
3
4
3
4
3
2
2
4
5
3
3
2
4
3
2
4
4
4
3
4
3
3
2
3
3
4
3
5
3
2
4
3
3
3
5
4
4
3
4
3
3
3
4
3
3
3
4
4
3
3
10
3
4
3
3
3
4
4
5
4
2
3
3
3
4
1
3
2
4
4
99
5
105
4
4
3
3
3
...

result:

wrong answer 1st numbers differ - expected: '4', found: '2'