QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#111072#6559. A Tree and Two EdgesOOBMABTRAMSTL 1ms6820kbC++172.1kb2023-06-05 19:11:142023-06-05 19:11:17

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:11:17]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:6820kb
  • [2023-06-05 19:11:14]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int mod=998244353;
typedef long long ll;
typedef long double ld;
int input=0;
const ll inf=2e18;
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,z=0;
    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);
    while(q--){
        cin>>s>>t,r=0;
        s=get(s),t=get(t);
        if(s==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();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0ms
memory: 6792kb

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
Time Limit Exceeded

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:


result: