QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#420625 | #961. Smol Vertex Cover | AFewSuns | TL | 0ms | 0kb | C++20 | 4.2kb | 2024-05-24 20:44:32 | 2024-05-24 20:44:33 |
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 8e18
#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;
queue<ll> q;
vector<ll> vec[1010];
ll t,n,m,head[550],cnt=0;
ll fa[550],mth[550],col[550],pre[550],dfn[550],tot=0;
ll dfnn[1010],low[1010],tott=0,blg[1010],cntt=0,st[1010],top=0;
bl ck[1010];
struct node{
ll nxt,to;
}e[250025];
void add(ll u,ll v){
e[++cnt].nxt=head[u];
e[cnt].to=v;
head[u]=cnt;
}
ll find(ll x){
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
}
il ll get_lca(ll x,ll y){
tot++;
x=find(x);
y=find(y);
while(dfn[x]!=tot){
dfn[x]=tot;
x=find(pre[mth[x]]);
if(y) swap(x,y);
}
return x;
}
il void blossom(ll x,ll y,ll lca){
while(find(x)!=lca){
pre[x]=y;
y=mth[x];
if(col[y]==2){
col[y]=1;
q.push(y);
}
if(find(x)==x) fa[x]=lca;
if(find(y)==y) fa[y]=lca;
x=pre[y];
}
}
il bl bfs(ll x){
fr(i,1,n) fa[i]=i;
fr(i,1,n) col[i]=pre[i]=0;
while(!q.empty()) q.pop();
q.push(x);
col[x]=1;
while(!q.empty()){
ll u=q.front();
q.pop();
go(u){
ll v=e[i].to;
if(find(u)==find(v)||col[v]==2) continue;
if(!col[v]){
col[v]=2;
pre[v]=u;
if(!mth[v]){
ll now=v,lst;
while(now){
lst=mth[pre[now]];
mth[now]=pre[now];
mth[pre[now]]=now;
now=lst;
}
return 1;
}
col[mth[v]]=1;
q.push(mth[v]);
}
else{
ll lca=get_lca(u,v);
blossom(u,v,lca);
blossom(v,u,lca);
}
}
}
return 0;
}
void tarjan(ll u){
dfnn[u]=low[u]=++tott;
st[++top]=u;
ck[u]=1;
fr(i,0,(ll)vec[u].size()-1){
ll v=vec[u][i];
if(!dfnn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(ck[v]) low[u]=min(low[u],dfnn[v]);
}
if(dfnn[u]==low[u]){
blg[u]=++cntt;
ck[u]=0;
while(st[top]!=u){
blg[st[top]]=cntt;
ck[st[top]]=0;
top--;
}
top--;
}
}
il void clr(){
fr(i,1,n) head[i]=mth[i]=dfn[i]=0;
cnt=tot=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);
}
ll ans=0;
fr(i,1,n) if(!mth[i]) ans+=bfs(i);
ll res=-1;
fr(x,0,n){
fr(i,1,2*n+2) vec[i].clear();
fr(i,1,2*n+2) dfnn[i]=low[i]=blg[i]=ck[i]=0;
tott=cntt=0;
fr(u,1,n){
go(u){
ll v=e[i].to;
vec[2*u+1].push_back(2*v+2);
}
}
fr(i,0,n){
if(!mth[i]){
if(i!=x) vec[2*i+2].push_back(2*i+1);
else vec[2*i+1].push_back(2*i+2);
}
else if(i<mth[i]){
if(i!=x&&mth[i]!=x){
vec[2*i+2].push_back(2*mth[i]+1);
vec[2*mth[i]+2].push_back(2*i+1);
}
if(i==x) vec[2*i+1].push_back(2*i+2);
if(mth[i]==x) vec[2*mth[i]+1].push_back(2*mth[i]+2);
}
}
fr(i,1,2*n+2) if(!dfnn[i]) tarjan(i);
bl pd=0;
fr(i,0,n) if(blg[2*i+1]==blg[2*i+2]) pd=1;
if(!pd){
res=x;
break;
}
}
if(res==-1) pf("not smol\n");
else if(!res){
writeln(ans);
fr(i,1,n) if(blg[2*i+2]<blg[2*i+1]) writesp(i);
enter;
}
else{
writeln(ans+1);
fr(i,1,n) if(blg[2*i+2]<blg[2*i+1]) writesp(i);
enter;
}
clr();
}
}
詳細信息
Test #1:
score: 0
Time Limit Exceeded
input:
5 5 1 2 2 3 3 4 4 5 1 5