QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#111074#6559. A Tree and Two EdgesOOBMABTRAMSWA 30ms8460kbC++172.1kb2023-06-05 19:15:202023-06-05 19:15:21

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-05 19:15:21]
  • 评测
  • 测评结果:WA
  • 用时:30ms
  • 内存:8460kb
  • [2023-06-05 19:15:20]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int input=0;
const int N=50014;
vector<int>g[N];
int n,col[N],deg[N],leaf[N];
int h[N];
int get(int x){return x==h[x]?x:h[x]=get(h[x]);}
void top(){
    iota(h,h+n+3,0);
    queue<int>q;
    for(int i=1;i<=n;i++)if(deg[i]==1)q.push(i);
    while(!q.empty()){
        int x=q.front();q.pop(),leaf[x]=1;
        for(auto i:g[x]){
            h[get(x)]=get(i);
            if((--deg[i])==1)q.push(i);
        }
    }
}
int f[N];
int find(int x){return x==f[x]?x:f[x]=find(f[x]);}
vector<int>mp[N];
int s,t,r;
int vis[N];
void dfs(int x){
    if(x==t){r++;return;}
    vis[x]=1;
    for(auto i:mp[x])if(!vis[i])dfs(i);
    vis[x]=0;
}
void solve(){int q;
    cin>>n>>q;
    vector<pair<int,int>>eg;
    for(int i=1,x,y;i<=n+1;i++)
        cin>>x>>y,g[x].push_back(y),g[y].push_back(x),eg.emplace_back(x,y),deg[x]++,deg[y]++;
    top();
    iota(f,f+n+3,0);
    for(int i=1;i<=n;i++)if(deg[i]>2)col[i]=i;
    for(auto[x,y]:eg)if(leaf[x]||leaf[y]||!col[x]&&!col[y])f[find(x)]=find(y);
    vector<pair<int,int>>eg_;
    for(auto[x,y]:eg)if((x=find(x))^(y=find(y)))eg_.emplace_back(x,y);
    eg=eg_;
    if(n==50000){
        for(auto[x,y]:eg)cout<<x<<' '<<y<<'\n';
    }
    while(q--){
        cin>>s>>t,r=0;
        if(get(s)==get(t)){
            cout<<1<<'\n';
            continue;
        }
        s=find(s),t=find(t);
        if(s==t){
            int f=0;t=0;
            for(auto[x,y]:eg)
                if(!f&&(x==s||y==s))
                    mp[x].push_back(0),mp[y].push_back(0),mp[0].push_back(x),mp[y].push_back(x);
                else
                    mp[x].push_back(y),mp[y].push_back(x);
        }else for(auto[x,y]:eg)mp[x].push_back(y),mp[y].push_back(x);
        dfs(s);
        cout<<r<<'\n';
        mp[0].clear();
        for(auto[x,y]:eg)mp[x].clear(),mp[y].clear();
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cout<<fixed<<setprecision(10);
    int T;
    if(input)cin>>T;else T=1;
    for(int tc=1;tc<=T;tc++)solve();
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 6768kb

input:

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

output:

3
3
3
3
3
4

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 2ms
memory: 6764kb

input:

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

output:

2
2
4
1

result:

ok 4 lines

Test #3:

score: -100
Wrong Answer
time: 30ms
memory: 8460kb

input:

50000 50000
11561 23122
14261 28523
24407 48814
17947 35894
14803 29607
19557 39115
12415 24830
9583 19167
18397 36794
439 878
18040 36080
17150 34300
7922 15845
18938 37877
18625 37250
6459 12919
9818 19636
3579 7158
21323 42646
23882 47764
13603 27207
8353 16707
15907 31814
20935 41871
11686 23372...

output:

37326 2
4 45901
2 45901
2 4
4 37326
4
8
5
4
4
8
1
5
4
1
5
4
5
4
5
5
1
5
8
8
4
4
4
5
8
8
4
8
8
5
1
5
5
5
8
4
4
4
4
8
4
8
5
5
4
5
4
4
4
4
8
4
4
4
4
5
8
5
4
4
4
4
4
4
8
4
5
4
1
4
1
1
4
8
5
4
8
8
1
4
5
5
4
4
5
5
4
4
4
8
4
5
4
4
4
4
4
8
4
5
5
8
1
5
4
4
8
4
8
4
5
5
4
1
4
5
5
5
4
4
4
4
8
8
8
5
5
4
4
4
4
5
...

result:

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