QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#634633 | #9453. Graph Generator | ucup-team4717# | TL | 996ms | 41648kb | C++20 | 2.3kb | 2024-10-12 17:46:53 | 2024-10-14 16:53:54 |
Judging History
answer
#include<bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define sd second
using namespace std;
const int N=2e5+5;
inline int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48,ch=getchar();}
return x*f;
}
int T,n,m,tot,in[N];
set<int> e[N];
set<pii,greater<pii > > s;
bool flg;
int vis[N],idx,siz,mx;
void dfs(int u){
vis[u]=idx,siz++;
for(int v:e[u]){
if(vis[v]!=idx){
dfs(v);
if(siz-1>mx) return;
}
}
}
void dfs2(int u){
vis[u]=idx;
for(int v:e[u])
if(vis[v]!=idx)
dfs2(v);
}
int id[N];
vector<int> ans[N];
int fa[N],sz[N],U[N],V[N],In[N];
inline int Find(int x){
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
map<int,int> mp;
map<pair<int,int> ,bool> lk;
inline void merge(int x,int y){
x=Find(x),y=Find(y);
if(x==y) return;
fa[x]=y,sz[y]+=sz[x];
}
signed main(){
T=read();
while(T--){
n=read(),m=read(),flg=0;
for(int u,v,i=1;i<=m;i++){
u=read(),v=read();
e[u].insert(v);
e[v].insert(u);
U[i]=u,V[i]=v;
in[u]++,in[v]++;
}
idx=1,tot=0,s.clear();
for(int i=1;i<=n;i++) s.insert({in[i],i});
while(!s.empty()){
int u=(*s.begin()).sd;
s.erase(s.begin());
for(int v:e[u]){
s.erase({in[v],v});
in[v]--;
s.insert({in[v],v});
e[v].erase(u);
}
for(int v:e[u])
ans[u].emplace_back(v);
id[++tot]=u;
}
lk.clear();
for(int i=1;i<=n;i++) fa[i]=i,sz[i]=1;
for(int i=tot;i;i--){
int f=id[i];
mp.clear();
vector<int> real;
int sum=0;
for(int x:ans[f]){
lk[{x,f}]=1;
if(!mp[Find(x)]) mp[Find(x)]=1,real.emplace_back(x),sum+=sz[Find(x)];
}
if(sum!=in[f]){
flg=1;
break;
}
ans[f]=real;
for(int x:ans[f]) merge(x,f);
}
if(lk.size()!=m) flg=1;
if(!flg){
for(int i=1;i<=m;i++){
if(!lk[{U[i],V[i]}]&&!lk[{V[i],U[i]}]){
flg=1;
break;
}
}
}
if(flg){
puts("No");
for(int i=1;i<=n;i++) vis[i]=0,in[i]=0,e[i].clear(),ans[i].clear();
continue;
}
puts("Yes");
for(int i=tot;i;i--){
int f=id[i];
printf("%d ",f);
printf("%d ",(int)ans[f].size());
for(int x:ans[f]) printf("%d ",x);
puts("");
}
for(int i=1;i<=n;i++) vis[i]=0,in[i]=0,e[i].clear(),ans[i].clear();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 18136kb
input:
3 3 0 4 4 1 2 2 3 3 4 2 4 5 5 1 2 2 3 3 4 4 5 2 4
output:
Yes 1 0 2 0 3 0 Yes 1 0 3 0 4 1 3 2 2 1 3 No
result:
ok 3 cases passed
Test #2:
score: 0
Accepted
time: 996ms
memory: 41648kb
input:
4176 10 15 1 4 9 1 3 7 8 1 2 1 5 4 5 1 10 1 7 1 10 7 10 3 6 4 3 1 6 1 9 2 7 10 6 7 1 7 1 4 7 2 4 2 5 2 3 1 1 6 2 6 5 1 6 8 5 2 3 1 4 6 2 1 5 1 1 4 6 2 6 1 9 15 5 1 4 2 7 1 1 8 2 3 5 8 1 9 4 3 6 5 9 2 3 1 4 1 7 2 9 7 1 6 6 11 1 2 3 5 3 1 3 2 4 3 1 6 4 2 1 4 5 4 5 1 5 2 6 6 1 6 6 3 1 3 1 5 4 2 2 1 10 ...
output:
Yes 2 0 3 0 5 0 6 0 8 0 7 1 3 9 1 2 4 2 5 6 10 1 3 1 4 2 3 4 8 No No No Yes 2 0 6 0 3 1 2 4 1 2 5 1 2 1 2 2 6 No No Yes 2 0 3 0 4 0 5 1 4 7 1 3 6 1 4 1 3 2 3 4 Yes 4 0 5 0 6 0 7 0 8 0 9 0 10 0 3 2 6 10 2 5 3 5 7 8 9 1 2 2 4 Yes 4 0 6 0 7 0 8 0 9 0 5 2 7 8 2 2 5...
result:
ok 4176 cases passed
Extra Test:
score: -3
Extra Test Failed : Time Limit Exceeded on 2
input:
28 49999 99999 26903 5029 23317 26903 23317 5029 4470 26903 4470 5029 23317 31323 31323 26903 31323 5029 31323 17955 23317 17955 17955 26903 17955 5029 5029 3731 5029 4863 14611 26903 5029 14611 26238 5029 4470 2789 2789 26903 2789 5029 5029 33449 27707 23317 26903 27707 5029 27707 23317 7484 26903 ...
output:
Yes 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 0 31 0 32 0 33 0 34 0 35 0 36 0 37 0 38 0 39 0 40 0 41 0 42 0 43 0 44 0 45 0 46 0 47 0 48 0 49 0 50 0 51 0 ...