QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#638967 | #9453. Graph Generator | tarjen | AC ✓ | 178ms | 13640kb | C++20 | 1.8kb | 2024-10-13 17:27:00 | 2024-10-13 17:27:01 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n,m;cin>>n>>m;
vector<vector<pair<int,int>>> ve(n+1);
for(int i=1;i<=m;i++){
int x,y;cin>>x>>y;
ve[x].emplace_back(y,i);
ve[y].emplace_back(x,i);
}
vector<int> siz(n+1,1);
vector<int> f(n+1);iota(f.begin(),f.end(),0);
function<int(int)> getf = [&](int x){
if(x==f[x])return x;
else return f[x]=getf(f[x]);
};
auto merge= [ &](int x,int y){
x=getf(x),y=getf(y);
if(x==y)return;
f[x]=y;
siz[y]+=siz[x];
};
vector<int>ok(m+1,1);
vector<int>id(n);iota(id.begin(),id.end(),1);
sort(id.begin(),id.end(),[&](int x,int y){
return ve[x].size()>ve[y].size();
}) ;
vector<vector<int>>ve2(n+1);
for(auto x:id){
for(auto [to,id]:ve[x])if(ok[id]){
ve2[x].push_back(to);
ok[id]=0;
}
}
vector<pair<int,vector<int>>> ans;
for(int i=n-1;i>=0;i--){
int x=id[i];
vector<int> v;
for(auto it:ve2[x])v.emplace_back(getf(it));
sort(v.begin(),v.end());v.resize(unique(v.begin(),v.end())-v.begin());
int sum=0;
for(auto it:v)sum+=siz[it];
// cout<<"x="<<x<<" sum="<<sum<<endl;
if(sum!=ve2[x].size()){
cout<<"No\n";return;
}
ans.emplace_back(x,v);
for(auto it:v)merge(it,x);
}
cout<<"Yes\n";
for(auto [x,v]:ans){
cout<<x<<" ";
cout<<v.size();
for(auto it:v)cout<<" "<<it;;cout<<"\n";
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;while(T--){
solve();
}
}
/*
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
*/
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3572kb
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 3 0 2 0 1 0 Yes 1 0 4 0 3 1 4 2 2 1 3 No
result:
ok 3 cases passed
Test #2:
score: 0
Accepted
time: 178ms
memory: 13640kb
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 8 0 9 0 6 0 5 0 2 1 9 10 0 7 1 10 4 2 5 6 3 1 7 1 4 2 3 4 8 No No No Yes 6 0 5 0 4 1 5 3 1 4 2 1 3 1 2 2 6 No No Yes 2 0 7 0 3 1 7 6 0 5 1 6 4 1 5 1 3 2 3 4 Yes 4 0 9 0 8 0 7 0 5 0 10 0 6 0 3 2 6 10 2 5 3 5 7 8 9 1 2 2 4 Yes 9 0 4 0 6 0 8 0 7 0 5 2 7 8 3 2 5 6 2 1 3 1 2 2 4 Yes 3 0 7 0 6 0 5 0 4...
result:
ok 4176 cases passed
Extra Test:
score: 0
Extra Test Passed