QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#168882#6559. A Tree and Two Edgesucup-team134#WA 24ms10324kbC++144.3kb2023-09-09 01:19:132023-09-09 01:19:13

Judging History

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

  • [2023-09-09 01:19:13]
  • 评测
  • 测评结果:WA
  • 用时:24ms
  • 内存:10324kb
  • [2023-09-09 01:19:13]
  • 提交

answer

#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define ll long long
using namespace std;
typedef pair<int,int> pii;

const int mod=998244353;
inline int add(int x,int y){int ret=x+y;if(ret>=mod)ret-=mod;return ret;}
inline int sub(int x,int y){int ret=x-y;if(ret<0)ret+=mod;return ret;}
inline int mul(int x,int y){return ((ll)x*y)%mod;}
inline int step(int base,int pw){int ret=1;while(pw){if(pw&1)ret=mul(ret,base);base=mul(base,base);pw>>=1;}return ret;}
inline int invv(int x){return step(x,mod-2);}

const int maxn=1e5+10;

set<pii>cedges;
vector<int>vect[maxn];
int cycdeg[maxn],color[maxn],pos[maxn],color_merged[maxn],comp_cdeg[maxn];
int n,q;

void fcyc(int x,int prv,vector<int>&stek){

    pos[x]=1;
    stek.pb(x);
    for(int i=0;i<vect[x].size();i++){
        int id=vect[x][i];
        if(id==prv)continue;
        if(pos[id]==1){

            cedges.insert({min(x,id),max(x,id)});
            int goal=id;
            /*for(int i=0;i<stek.size();i++){
                printf("%d ",stek[i]);
            }
            printf(" STEK\n");*/
            for(int i=stek.size()-2;i>=0;i--){
                cedges.insert({min(stek[i],stek[i+1]),max(stek[i],stek[i+1])});
                if(stek[i]==goal)break;
            }
            continue;
        }
        if(pos[id])continue;
        fcyc(id,x,stek);
    }

    pos[x]=2;
    stek.pop_back();
}
int cmask[maxn];
int cyc_color[maxn];
void bojaj_dfs(int x,int cl){

    pos[x]=1;
    color[x]=cl;
    cmask[cl]|=cyc_color[x];
    for(int i=0;i<vect[x].size();i++){
        int id=vect[x][i];
        if(pos[id] || (cycdeg[id]>0 && cycdeg[x]>0))continue;
        bojaj_dfs(id,cl);
    }
}
void obojaj(){

    memset(pos,0,sizeof(pos));
    for(int i=1;i<=n;i++){
        if(cycdeg[i]==0 || pos[i])continue;
        bojaj_dfs(i,i);
    }

}
void bojaj_dfs_merged(int x,int cl){

    pos[x]=1;
    color_merged[x]=cl;
    for(int i=0;i<vect[x].size();i++){
        int id=vect[x][i];
        if(pos[id] || cycdeg[id]==3)continue;
        bojaj_dfs_merged(id,cl);
    }
}
void obojaj_merged(){

    memset(pos,0,sizeof(pos));
    for(int i=1;i<=n;i++){
        if(cycdeg[i]==3 || pos[i])continue;
        bojaj_dfs_merged(i,i);
    }

}
void go_merge(){

    obojaj();
    obojaj_merged();

    while(q--){

        int u,v;
        scanf("%d %d",&u,&v);

        if(color[u]==color[v])printf("1\n");
        else{

            u=color[u];
            v=color[v];
            if(cycdeg[u]>cycdeg[v])swap(u,v);

            if(cycdeg[u]==3 && cycdeg[v]==3)printf("3\n");
            else if(cycdeg[u]==2 && cycdeg[v]==3)printf("3\n");
            else{
                if(color_merged[u]==color_merged[v])printf("3\n");
                else printf("4\n");
            }

        }

    }

}
void dfs_cikluse(int x,int prv,int cl){

    cyc_color[x]|=(1<<cl);
    pos[x]=1;
    for(int i=0;i<vect[x].size();i++){
        int id=vect[x][i];
        if(pos[id] || cycdeg[id]==0 || (cycdeg[x]==4) )continue;
        dfs_cikluse(id,x,cl);
    }

}
void obojaj_cikluse(){

    memset(pos,0,sizeof(pos));
    int cnt=1;
    for(int i=1;i<=n;i++){
        if(pos[i] || cycdeg[i]==0 || cycdeg[i]==4)continue;
        dfs_cikluse(i,0,cnt);
        cnt++;
    }

}
void go_split(){

    obojaj_cikluse();
    obojaj();

    while(q--){

        int u,v;
        scanf("%d %d",&u,&v);

        if(color[u]==color[v])printf("1\n");
        else{

            u=color[u];
            v=color[v];

            ///printf("%d %d AAAA\n",cmask[u],cmask[v]);

            if(cmask[u]&cmask[v])printf("2\n");
            else printf("4\n");

        }

    }


}
int main(){


    ///freopen("test.txt","r",stdin);

    scanf("%d %d",&n,&q);
    for(int i=1;i<=n+1;i++){
        int u,v;
        scanf("%d %d",&u,&v);
        vect[u].pb(v);
        vect[v].pb(u);
    }

    vector<int>stek;
    fcyc(1,0,stek);

    for(set<pii>::iterator it=cedges.begin();it!=cedges.end();it++){
        cycdeg[(*it).ff]++;
        cycdeg[(*it).ss]++;
        ///printf("%d %d CEDGE\n",(*it).ff,(*it).ss);
    }

    int cas=0;
    for(int i=1;i<=n;i++)if(cycdeg[i]==3)cas=1;

    if(cas==1)go_merge();
    else go_split();


    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 8576kb

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

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: 0
Accepted
time: 24ms
memory: 9028kb

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:

4
3
3
4
4
3
1
3
4
1
3
4
3
4
3
3
1
3
3
3
4
4
4
3
3
3
4
3
3
3
1
3
3
3
3
4
4
4
4
3
4
3
3
3
4
3
4
4
4
4
3
4
4
4
4
3
3
3
4
4
4
4
4
4
3
4
3
4
1
4
1
1
4
3
3
4
3
3
1
4
3
3
4
4
3
3
4
4
4
3
4
3
4
4
4
4
4
3
4
3
3
3
1
3
4
4
3
4
3
4
3
3
4
1
4
3
3
3
4
4
4
4
3
3
3
3
3
4
4
4
4
3
3
4
3
4
4
3
3
4
4
4
1
3
3
3
3
4
4
3
...

result:

ok 50000 lines

Test #4:

score: 0
Accepted
time: 21ms
memory: 10324kb

input:

50000 50000
1730 3460
17535 35071
14108 28216
20630 41260
2091 4182
8112 16225
21373 42746
6685 13371
21142 42284
12168 24337
22564 45128
16103 32207
9254 18508
21369 42739
1955 3911
13696 27392
3929 7858
1777 3555
23382 46764
830 1660
17722 35444
11495 22991
10184 20369
13697 27395
24728 49456
4037...

output:

1
1
3
3
3
4
4
4
4
4
3
3
3
4
1
3
4
3
3
1
3
3
4
3
1
4
3
3
4
3
3
4
4
4
1
1
4
1
3
4
3
1
4
4
3
3
3
4
1
4
4
1
3
1
3
3
3
1
1
3
3
3
3
3
4
3
4
4
3
3
4
4
4
3
3
4
4
4
3
4
3
3
3
3
3
3
3
3
4
4
1
4
3
4
1
4
4
4
4
3
4
1
4
4
3
4
3
4
3
4
3
1
4
3
1
1
3
3
4
4
1
3
3
3
4
3
3
4
4
4
4
4
3
3
4
3
4
1
1
3
4
4
3
4
4
3
3
4
4
3
...

result:

ok 50000 lines

Test #5:

score: -100
Wrong Answer
time: 14ms
memory: 9016kb

input:

50000 50000
21879 43758
12510 25020
2593 5187
16048 32096
9697 19394
12606 25212
3860 7720
8231 16462
23983 47966
10852 21705
6919 13839
1385 2770
4040 8080
14298 28596
22248 44496
4245 8490
14486 28972
11445 22891
21557 43114
20946 41892
23374 46749
78 157
4617 9234
8198 16396
12228 24456
16125 322...

output:

2
2
2
2
2
2
1
2
2
2
2
2
1
2
2
2
2
2
2
2
2
1
2
2
2
2
2
2
2
2
1
2
2
2
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
2
1
2
2
2
2
2
2
2
2
2
2
2
2
1
2
1
2
2
2
2
2
2
2
2
2
2
2
1
2
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
2
2
2
2
1
2
2
1
2
2
2
2
2
2
2
2
2
2
2
1
2
1
2
2
2
2
2
2
2
2
2
2
...

result:

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