QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#490695 | #8757. 图 | DEMONKILLER | RE | 63ms | 3780kb | C++14 | 1.9kb | 2024-07-25 16:07:45 | 2024-07-25 16:07:45 |
Judging History
answer
#include<cstdio>
#include<vector>
#define N 100010
#define Pii pair<int,int>
using namespace std;
struct node{
int to,next;
}edge[N<<1];
int T,n,m,k,cnt,nxt[N],head[N];
vector<int> num;
vector<vector<int> > fa,ans;
vector<vector<Pii> > Edge;
void add(int from,int to){
edge[++cnt]={to,head[from]};
head[from]=cnt;
}
void init(){
num.clear();
fa.clear();
ans.clear();
Edge.clear();
}
int find(int s,int x){
return fa[s][x]==x?x:fa[s][x]=find(s,fa[s][x]);
}
void dfs(int now,int fat){
nxt[now]=fat;
for(int i=head[now];i;i=edge[i].next)
if(edge[i].to!=fat)dfs(edge[i].to,now);
}
void solve(){
scanf("%d%d",&n,&m);
k=(m-1)/(n-1)+1;
init();
for(int i=1;i<=n;i++)num.emplace_back(i-1);
for(int i=1;i<=m;i++){
int u,v,l=0,r=fa.size()-1,Res=-1;
scanf("%d%d",&u,&v);
while(l<=r){
int mid=(l+r)>>1;
if(find(mid,u-1)!=find(mid,v-1)){
r=mid-1;
Res=mid;
}
else l=mid+1;
}
if(Res==-1){
Res=fa.size();
fa.emplace_back(num);
Edge.emplace_back();
}
fa[Res][find(Res,u-1)]=find(Res,v-1);
Edge[Res].emplace_back(u,v);
if(fa.size()==k)break;
}
Pii st=Edge[k-1][0];
for(int i=1;i<=k;i++){
cnt=0;
for(int j=1;j<=n;j++)head[j]=0;
for(auto [u,v]:Edge[i-1])
add(u,v),add(v,u);
dfs(st.second,0);
ans.emplace_back();
for(int j=st.first;j;j=nxt[j])
ans[i-1].emplace_back(j);
}
printf("%d %d\n",st.first,st.second);
for(int i=1;i<=k;i++){
printf("%d",ans[i-1].size());
for(int x:ans[i-1])
printf(" %d",x);
putchar('\n');
}
}
int main(){
scanf("%d",&T);
while(T--)solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 63ms
memory: 3780kb
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:
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 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 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 ...
result:
ok Answer correct. (10000 test cases)
Test #2:
score: -100
Runtime Error
input:
10000 5 20 2 1 2 5 5 3 3 1 4 5 1 4 4 3 4 5 3 5 5 4 2 3 5 2 3 4 3 5 1 4 4 3 4 2 2 1 1 3 5 1 5 20 4 2 1 3 1 2 4 5 2 4 3 1 5 3 5 1 4 5 4 3 2 4 1 4 4 3 5 2 1 2 3 5 1 5 4 1 3 4 4 3 5 20 1 4 1 3 1 5 5 1 4 5 3 4 4 5 2 3 1 2 2 4 4 5 4 5 2 4 2 5 4 2 4 3 4 2 2 5 2 1 3 1 5 20 2 5 2 3 4 5 4 2 3 4 2 1 5 4 2 5 2 ...