QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#123549 | #6413. Classical Graph Theory Problem | AFewSuns | WA | 16ms | 11728kb | C++14 | 4.4kb | 2023-07-12 20:34:49 | 2023-07-12 20:34:53 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
namespace my_std{
#define ll long long
#define bl bool
ll my_pow(ll a,ll b,ll mod){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
ll qpow(ll a,ll b){
ll res=1;
if(!b) return 1;
while(b){
if(b&1) res*=a;
a*=a;
b>>=1;
}
return res;
}
#define db double
#define pf printf
#define pc putchar
#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
#define go(u) for(ll i=head[u];i;i=e[i].nxt)
#define enter pc('\n')
#define space pc(' ')
#define fir first
#define sec second
#define MP make_pair
#define il inline
#define inf 1e18
#define random(x) rand()*rand()%(x)
#define inv(a,mod) my_pow((a),(mod-2),(mod))
il ll read(){
ll sum=0,f=1;
char ch=0;
while(!isdigit(ch)){
if(ch=='-') f=-1;
ch=getchar();
}
while(isdigit(ch)){
sum=sum*10+(ch^48);
ch=getchar();
}
return sum*f;
}
il void write(ll x){
if(x<0){
x=-x;
pc('-');
}
if(x>9) write(x/10);
pc(x%10+'0');
}
il void writeln(ll x){
write(x);
enter;
}
il void writesp(ll x){
write(x);
space;
}
}
using namespace my_std;
vector<pair<ll,ll> > vec[200020];
ll t,n,m,head[200020],cnt=1,d[200020],lf[200020],ans[200020];
bl ck[500050],vis[200020];
struct node{
ll nxt,to;
}e[1000010];
void add(ll u,ll v){
e[++cnt].nxt=head[u];
e[cnt].to=v;
head[u]=cnt;
}
il ll chk(ll l,ll r){
if(l<=-1&&-1<=r&&abs(l)%2==1) return -1;
if(l<=0&&0<=r&&abs(l)%2==0) return 0;
if(l<=1&&1<=r&&abs(l)%2==1) return 1;
return inf;
}
il void clr(){
fr(i,1,n) head[i]=0;
cnt=1;
fr(i,1,n) d[i]=lf[i]=ans[i]=vis[i]=0;
fr(i,1,n) vec[i].clear();
fr(i,1,m) ck[i]=0;
}
int main(){
t=read();
while(t--){
n=read();
m=read();
fr(i,1,m){
ll u=read(),v=read();
add(u,v);
add(v,u);
d[u]++;
d[v]++;
}
fr(u,1,n){
if(d[u]!=1) continue;
go(u){
ll v=e[i].to;
lf[v]++;
}
}
fr(j,1,m){
ll u=e[2*j].to,v=e[2*j+1].to;
if(d[u]==1||d[v]==1) continue;
bl pd=1;
if(d[u]==2){
go(u){
ll vv=e[i].to;
if(!ck[i>>1]&&vv!=v&&lf[vv]==2) pd=0;
}
}
if(d[v]==2){
go(v){
ll vv=e[i].to;
if(!ck[i>>1]&&vv!=u&&lf[vv]==2) pd=0;
}
}
if(!pd) continue;
ck[j]=1;
d[u]--;
d[v]--;
if(d[u]==1){
go(u){
ll vv=e[i].to;
if(!ck[i>>1]) lf[vv]++;
}
}
if(d[v]==1){
go(v){
ll vv=e[i].to;
if(!ck[i>>1]) lf[vv]++;
}
}
}
fr(u,1,n){
if(!lf[u]&&d[u]==2){
ll x=0,y=0;
go(u){
ll v=e[i].to;
if(ck[i>>1]) continue;
if(!x) x=v;
else y=v;
}
vec[x].push_back(MP(y,u));
vec[y].push_back(MP(x,u));
}
}
ll sum=0;
fr(x,1,n){
if(vis[x]||!lf[x]) continue;
vis[x]=1;
if(lf[x]==1){
go(x){
ll v=e[i].to;
if(ck[i>>1]) continue;
ans[v]=vis[v]=1;
}
continue;
}
ll cnt1=0,cnt0=0;
fr(i,0,(ll)vec[x].size()-1){
ll v=vec[x][i].fir;
if(!vis[v]) continue;
if(ans[v]) cnt1++;
else cnt0++;
}
ll tmp=chk(sum-cnt1-1-cnt0,sum-cnt1-1+cnt0);
if(tmp!=inf){
ll tmpsum=tmp;
tmp=(tmp-(sum-cnt1-1-cnt0))/2;
sum=tmpsum;
ans[x]=1;
fr(i,0,(ll)vec[x].size()-1){
ll v=vec[x][i].fir;
if(!vis[v]) continue;
if(ans[v]) ans[vec[x][i].sec]=0;
else if(tmp){
ans[vec[x][i].sec]=1;
tmp--;
}
else ans[vec[x][i].sec]=0;
}
go(x){
ll v=e[i].to;
if(!ck[i>>1]&&d[v]==1){
ans[v]=0;
vis[v]=1;
}
}
}
else{
tmp=chk(sum+cnt0+1-cnt1,sum+cnt0+1+cnt1);
ll tmpsum=tmp;
tmp=(tmp-(sum+cnt0+1-cnt1))/2;
sum=tmpsum;
ans[x]=0;
fr(i,0,(ll)vec[x].size()-1){
ll v=vec[x][i].fir;
if(!vis[v]) continue;
if(!ans[v]) ans[vec[x][i].sec]=1;
else if(tmp){
ans[vec[x][i].sec]=1;
tmp--;
}
else ans[vec[x][i].sec]=0;
}
go(x){
ll v=e[i].to;
if(!ck[i>>1]&&d[v]==1) ans[v]=vis[v]=1;
}
}
}
if(sum<=0){
fr(i,1,n) if(ans[i]) writesp(i);
}
else{
fr(i,1,n) if(!ans[i]) writesp(i);
}
enter;
clr();
}
}
/*
3
2 1
1 2
6 7
1 2
1 3
2 3
3 4
4 5
4 6
5 6
3 2
1 2
2 3
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 11728kb
input:
2 6 7 1 2 1 3 2 3 3 4 4 5 4 6 5 6 3 2 1 2 2 3
output:
3 4 5 2
result:
ok ok (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 16ms
memory: 11312kb
input:
10000 2 1 1 2 29 28 13 19 16 5 21 7 22 10 10 2 1 18 27 13 10 3 11 23 12 22 11 7 7 17 29 17 9 1 28 21 2 18 13 9 4 25 20 16 5 14 20 7 14 4 12 8 8 24 17 19 15 1 11 6 26 9 13 12 13 9 12 2 6 12 9 11 5 2 8 10 6 10 3 10 7 1 7 5 8 9 4 1 12 11 10 6 2 8 12 4 5 10 11 1 3 1 10 1 12 9 9 1 8 3 7 1 35 35 13 8 34 1...
output:
2 10 11 14 15 18 19 20 22 24 25 26 27 28 29 4 5 10 11 12 13 1 2 3 4 9 10 3 5 9 13 15 16 18 23 24 28 29 30 31 32 33 34 35 4 5 8 9 10 12 13 14 15 2 3 4 3 4 7 9 13 14 20 21 22 24 27 28 31 33 35 37 40 42 43 44 45 46 47 48 50 1 2 6 8 12 13 15 18 20 21 22 23 27 29 30 31 33 36 1 3 4 7 8 10 13 5 ...
result:
wrong answer vertex 3 is repeated twice in the output (test case 394)