QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#129694 | #4818. Inverse Line Graph | DerekFeng | WA | 111ms | 22384kb | C++23 | 2.5kb | 2023-07-22 22:12:21 | 2023-07-22 22:12:24 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int N=3e5+4,M=3e6+4;
int n,m,A[N],B[N],S,tot;vector<int>gr[N],vec;
bool vis[N];int cnt[N],ls[N],num;
vector<int>v[N];map<pii,int>mp;int dfn[N],tg;
void dfs(int x){
vis[x]=1,S+=gr[x].size(),vec.emplace_back(x);
for(auto&y:gr[x])if(!vis[y])dfs(y);
}
bool check(int x,vector<int>a,vector<int>b){
for(auto&i:vec)cnt[i]=0;
for(int i=3;i<gr[x].size();i++){
int y=gr[x][i];
if(mp.count({a[1],y})&&mp.count({a[2],y}))a.emplace_back(y);
else b.emplace_back(y);
}
cnt[x]=2;queue<int>que;
for(auto&y:gr[x])cnt[y]=1,que.push(y);
num=0;v[++num]=a,v[++num]=b;
for(auto&x:a)ls[x]=1;for(auto&x:b)ls[x]=2;
while(!que.empty()){
int u=que.front();que.pop();if(cnt[u]>1)continue;
cnt[u]++;if(v[ls[u]].size()-1>gr[u].size())return 0;
++tg;for(auto&w:v[ls[u]])dfn[w]=tg;
v[++num]={u};for(auto&w:gr[u])if(dfn[w]!=tg){
v[num].emplace_back(w),ls[w]=num,cnt[w]++;
if(cnt[w]<2)que.push(w);
}
}
for(auto&i:vec)if(cnt[i]!=2)return 0;
long long sum=0;
for(int i=1;i<=num;i++)sum+=(long long)(v[i].size()-1)*v[i].size();
if(sum!=S)return 0;
for(int i=1;i<=num;i++)
for(auto&u:v[i])for(auto&w:v[i])
if(u!=w&&!mp.count({u,w}))return 0;
return 1;
}
void calc(){
for(auto&x:vec)cnt[x]=0;
for(int i=1;i<=num;i++)for(auto&x:v[i]){
if(cnt[x]==0)A[x]=tot+i;
if(cnt[x]==1)B[x]=tot+i;
cnt[x]++;
}
tot+=num;
}
void sol(){
cin>>n>>m,tot=0;
for(int i=1;i<=n;i++)gr[i].clear(),vis[i]=0;
mp.clear();while(m--){
int u,v;cin>>u>>v;mp[{u,v}]=mp[{v,u}]=1;
gr[u].emplace_back(v),gr[v].emplace_back(u);
}
for(int i=1;i<=n;i++)if(!vis[i]){
vec.clear(),S=0,dfs(i);
if(gr[i].size()==0){A[i]=++tot,B[i]=++tot;continue;}
if(gr[i].size()==1)if(check(i,{i,gr[i][0]},{i})){calc();continue;}
if(gr[i].size()==2){
if(check(i,{i,gr[i][0],gr[i][1]},{i})){calc();continue;}
if(check(i,{i,gr[i][0]},{i,gr[i][1]})){calc();continue;}
}
if(gr[i].size()>2){
if(check(i,{i,gr[i][0],gr[i][1],gr[i][2]},{i})){calc();continue;}
if(check(i,{i,gr[i][0],gr[i][1]},{i,gr[i][2]})){calc();continue;}
if(check(i,{i,gr[i][0],gr[i][2]},{i,gr[i][1]})){calc();continue;}
if(check(i,{i,gr[i][1],gr[i][2]},{i,gr[i][0]})){calc();continue;}
}
cout<<"No\n";
return;
}
cout<<"Yes\n"<<tot<<' '<<n<<'\n';
for(int i=1;i<=n;i++)cout<<A[i]<<' '<<B[i]<<'\n';
}
int main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int tc;cin>>tc;while(tc--)sol();
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 22060kb
input:
6 5 6 1 2 1 3 1 4 3 4 2 5 4 5 1 0 2 1 1 2 3 3 1 2 1 3 2 3 4 3 1 2 1 3 1 4 5 6 1 2 2 3 2 4 3 4 3 5 4 5
output:
Yes 5 5 1 2 2 3 1 4 1 5 3 5 Yes 2 1 1 2 Yes 3 2 1 2 1 3 Yes 4 3 1 2 1 3 1 4 No Yes 5 5 1 2 1 3 3 4 3 5 4 5
result:
ok that's great! (sum n = 20, sum m = 19) (6 test cases)
Test #2:
score: 0
Accepted
time: 5ms
memory: 22224kb
input:
5 6 5 3 4 1 6 1 5 2 5 5 6 2 0 1 0 5 3 2 3 3 4 2 4 6 4 5 6 4 5 1 6 2 4
output:
Yes 8 6 1 2 4 5 6 7 6 8 1 4 1 3 Yes 4 2 1 2 3 4 Yes 2 1 1 2 Yes 8 5 1 2 3 4 3 5 3 6 7 8 Yes 8 6 1 2 5 6 7 8 4 5 3 4 1 3
result:
ok that's great! (sum n = 20, sum m = 12) (5 test cases)
Test #3:
score: 0
Accepted
time: 2ms
memory: 21920kb
input:
14 5 3 2 5 1 5 1 2 4 3 2 4 1 2 1 3 5 6 2 3 1 5 4 5 2 4 1 2 1 3 3 0 6 5 3 4 1 5 2 6 5 6 2 3 4 4 1 2 3 4 1 4 2 3 6 7 1 3 5 6 3 6 1 2 3 5 1 6 2 6 1 0 5 5 1 2 2 4 2 5 3 5 4 5 4 0 2 0 3 0 1 0 1 0
output:
Yes 8 5 1 2 1 4 5 6 7 8 1 3 Yes 5 4 1 2 1 3 2 4 3 5 Yes 5 5 1 2 1 4 1 5 3 4 2 3 Yes 6 3 1 2 3 4 5 6 Yes 7 6 1 2 4 5 5 6 6 7 1 3 3 4 Yes 4 4 1 2 1 3 3 4 2 4 Yes 7 6 1 2 1 4 2 3 6 7 3 5 1 3 Yes 2 1 1 2 Yes 6 5 1 2 1 3 5 6 3 4 3 5 Yes 8 4 1 2 3 4 5 6 7 8 Yes 4 2 1 2 3 4 Yes 6 3 1 2 3 4 5 6 Yes 2 1 1 2 ...
result:
ok that's great! (sum n = 50, sum m = 33) (14 test cases)
Test #4:
score: 0
Accepted
time: 0ms
memory: 22384kb
input:
5 5 3 2 3 1 2 2 5 9 12 2 6 2 5 3 5 3 4 5 8 2 8 6 9 4 7 1 9 1 8 7 9 1 6 2 0 5 3 4 5 1 3 3 4 2 1 1 2
output:
No Yes 8 9 1 2 4 5 7 8 6 8 4 7 1 5 3 6 2 4 1 3 Yes 4 2 1 2 3 4 Yes 7 5 1 2 6 7 1 3 3 4 4 5 Yes 3 2 1 2 1 3
result:
ok that's great! (sum n = 23, sum m = 19) (5 test cases)
Test #5:
score: -100
Wrong Answer
time: 111ms
memory: 20656kb
input:
61395 7 0 1 0 5 1 1 2 3 1 1 3 7 11 2 4 2 5 1 6 5 7 1 4 1 5 2 7 3 7 1 2 3 5 4 5 9 10 2 7 4 5 5 9 1 8 1 3 2 6 6 7 2 4 3 7 6 8 1 0 8 10 2 6 6 8 1 4 1 8 4 6 2 4 2 3 1 2 4 8 1 3 9 12 3 7 4 7 7 9 1 6 3 8 2 5 5 9 3 4 4 6 4 8 5 7 7 8 6 5 2 6 1 2 4 5 2 3 1 3 3 1 1 2 2 0 1 0 4 2 1 2 2 4 3 3 1 2 2 3 1 3 1 0 9 ...
output:
Yes 14 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Yes 2 1 1 2 Yes 9 5 1 2 1 3 4 5 6 7 8 9 Yes 5 3 1 2 4 5 1 3 Yes 7 7 1 2 1 6 5 7 1 4 1 5 2 3 5 6 Yes 9 9 1 2 5 6 2 4 6 7 7 8 3 5 4 5 1 3 8 9 Yes 2 1 1 2 Yes 9 8 1 2 2 3 2 5 1 3 6 7 3 4 8 9 1 4 Yes 10 9 1 2 9 10 4 6 3 4 5 9 1 3 4 5 4 7 5 8 Yes 8 6 1 2 1 3 1 4 ...
result:
wrong answer duplicated edge (3, 5) (test case 19281)