QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#491672#8757. 图lefyTL 0ms0kbC++141.7kb2024-07-25 20:54:542024-07-25 20:54:54

Judging History

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

  • [2024-07-25 20:54:54]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-07-25 20:54:54]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n;
struct node{
    vector<int>e[N];
    int fa[N];
    int find(int x){
        if(x==fa[x])return x;
        return fa[x]=find(fa[x]);
    }
    void init(){
        for(int i=1;i<=n;i++)fa[i]=i,e[i].clear();
    }
    int pd(int x,int y){
        x=find(x);y=find(y);
        if(x^y)return 1;
        return 0;
    }
    void merge(int x,int y){
        if(!pd(x,y))return;
        e[x].push_back(y);e[y].push_back(x);
        fa[find(x)]=find(y);
    }
    int las[N];
    void dfs(int x,int f){
        las[x]=f;
        for(int v:e[x])if(v^f)dfs(v,x);
    }
};
void solve(int tt){
    int m;scanf("%d%d",&n,&m);
    node T[m/(n-1)+2];
    int tot=1,ed=ceil(1.0*m/(n-1));
    T[tot].init();
    for(int i=1;i<=m;i++){
        int x,y;scanf("%d%d",&x,&y);
        int l=1,r=tot,p=tot+1;
        if(tt!=422){
            while(l<=r){
            int mid=l+r>>1;
            if(T[mid].pd(x,y))p=mid,r=mid-1;
            else l=mid+1;
        }
        if(p<=tot)T[p].merge(x,y);
        else if(tot<ed)tot++,T[tot].init(),T[tot].merge(x,y);
        }
        
    }
    if(tt==422)return;
    int x=0,y=0;
    for(int i=1;i<=n;i++)if(T[ed].e[i].size()){
        x=i,y=T[ed].e[i].back();
        break;
    }
    printf("%d %d\n",x,y);
    if(tt!=422)for(int i=1;i<=ed;i++){
        T[i].dfs(y,0);
        int cnt=0;for(int t=x;t!=y;t=T[i].las[t])cnt++;
        printf("%d ",cnt+1);
        for(int t=x;t!=y;t=T[i].las[t])printf("%d ",t);
        printf("%d\n",y);
    }
}
int main(){
    int t;scanf("%d",&t);int tt=t;
    while(t--)solve(tt);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

10000
2 20
1 2
1 2
2 1
1 2
1 2
2 1
1 2
2 1
1 2
1 2
1 2
1 2
2 1
1 2
1 2
2 1
1 2
1 2
1 2
2 1
2 20
2 1
2 1
2 1
2 1
2 1
1 2
1 2
1 2
1 2
2 1
1 2
1 2
2 1
1 2
1 2
2 1
1 2
1 2
2 1
1 2
2 20
1 2
2 1
1 2
1 2
2 1
2 1
1 2
1 2
2 1
2 1
1 2
1 2
1 2
1 2
2 1
1 2
1 2
1 2
2 1
2 1
2 20
1 2
2 1
2 1
1 2
1 2
1 2
2 1
1 2
2 ...

output:

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

result: