QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#425400 | #961. Smol Vertex Cover | oceeff | TL | 25ms | 4288kb | C++14 | 3.4kb | 2024-05-30 10:25:38 | 2024-05-30 10:25:42 |
Judging History
answer
/*
为什么我们可以直接跑sat?因为我们求出的是最大匹配,所以任意匹配的两端不可能都有相邻的未选点,否则容易增大匹配个数
因此对于每组匹配,我们将有相邻的未选点的那一个点(如果没有就随便)
于是可以调整方案使得没有相邻的点都取真!
3
5 5
1 2
2 3
3 4
4 5
1 5
3 0
5 10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
3
2 3 5
0
not smol
*/
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
#endif
using namespace std;int read(){int num=0,f=1;char c;while(!isdigit(c=getchar()))if(c=='-')f=-1;while(isdigit(c))num=num*10+(c&15),c=getchar();return num*f;}void write(int x,char ch=' '){int F[20],cnt=0;if(!x)putchar('0');if(x<0)putchar('-'),x=-x;while(x)F[cnt++]=x%10+'0',x/=10;while(cnt)putchar(F[--cnt]);putchar(ch);}
namespace Main
{
const int N=510,M=N*N;
struct SAT{int n,k,q,ti,dfn[N<<1],low[N<<1],st[N<<1],scc[N<<1],top/*,head[N<<1],to[M<<2],nxt[M<<2],tot*/;vector<int>e[N<<1];void clear(int nn){n=nn,k=n>>1,memset(dfn,0,(n+1)<<2),memset(scc,0,(n+1)<<2),q=ti=top=0;for(int i=1;i<=n;++i)e[i].clear();}void link(int x,int y){e[x].push_back(y);}
void tarjan(int x){dfn[x]=low[x]=++ti,st[++top]=x;for(auto y:e[x])if(!dfn[y])tarjan(y),low[x]=min(low[x],low[y]);else if(!scc[y])low[x]=min(low[x],dfn[y]);if(dfn[x]==low[x])for(++q,st[top+1]=0;st[top+1]!=x;--top)scc[st[top]]=q;}bool cal(){for(int i=1;i<=n;++i)if(!dfn[i])tarjan(i);for(int i=1;i<=k;++i)if(scc[i]==scc[i+k])return 0;return 1;}bool C(int x){return scc[x]>scc[x+k];}}A;
int n,m,q,x,y,z,head[N],to[M],nxt[M],tot,ti,id[N],fa[N],mp[N],pre[N],vis[N];vector<int>ans;queue<int>que;int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}void link(int x,int y){to[++tot]=y,nxt[tot]=head[x],head[x]=tot;}
int lca(int x,int y){for(++ti,x=find(x),y=find(y);id[x]!=ti;){id[x]=ti,x=find(pre[mp[x]]);if(y)swap(x,y);}return x;}void blo(int x,int y,int z){for(;find(x)!=z;){pre[x]=y,y=mp[x];if(vis[y]==2)vis[y]=1,que.push(y);if(find(x)==x)fa[x]=z;if(find(y)==y)fa[y]=z;x=pre[y];}}
bool aug(int s){for(int i=1;i<=n;++i)fa[i]=i;for(memset(vis,0,(n+1)<<2),memset(pre,0,(n+1)<<2);que.size();que.pop());for(vis[s]=1,que.push(s);que.size();){x=que.front(),que.pop();for(int i=head[x];i;i=nxt[i])if(find(x)!=find(y=to[i])&&vis[y]!=2){if(!vis[y]){vis[y]=2,pre[y]=x;if(!mp[y]){for(int j=y;j;j=x)x=mp[pre[j]],mp[mp[j]=pre[j]]=j;return 1;}vis[mp[y]]=1,que.push(mp[y]);}else z=lca(x,y),blo(x,y,z),blo(y,x,z);}}return 0;}
void clr(){memset(head,0,(n+1)<<2),memset(mp,0,(n+1)<<2),q=tot=0;}bool calc(int x){A.clear(n<<1);for(int i=1;i<=n;++i)for(int j=head[i];j;j=nxt[j])A.link(i,to[j]+n);for(int i=1;i<=n;++i)if(!mp[i])(i!=x?A.link(i+n,i):A.link(i,i+n));else if(i!=x&&mp[i]!=x)A.link(i+n,mp[i]);return A.cal();}
void main(){for(n=read(),m=read(),clr();m--;)x=read(),y=read(),link(x,y),link(y,x);for(int i=1;i<=n;++i)if(!mp[i])aug(i);for(int i=0;i<=n;++i)if(calc(i)){ans.clear();for(int j=1;j<=n;++j)if(A.C(j))ans.push_back(j);write((int)ans.size(),'\n');for(auto j:ans)write(j);putchar('\n');return;}printf("not smol\n");}
};
int main()
{
// freopen(".in","r",stdin),freopen(".out","w",stdout);
const bool base=0,IO=1;int T;if(!base)T=1;else if(IO)T=read();else ios::sync_with_stdio(0),cin>>T;for(;T--;)Main::main();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3940kb
input:
5 5 1 2 2 3 3 4 4 5 1 5
output:
3 1 3 5
result:
ok vertex cover of size 3
Test #2:
score: 0
Accepted
time: 1ms
memory: 3856kb
input:
5 10 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 4 5
output:
not smol
result:
ok not smol
Test #3:
score: 0
Accepted
time: 1ms
memory: 3908kb
input:
3 0
output:
0
result:
ok vertex cover of size 0
Test #4:
score: 0
Accepted
time: 1ms
memory: 3700kb
input:
10 10 2 5 3 8 3 10 6 9 1 4 2 6 2 3 4 6 7 10 4 7
output:
5 3 4 5 6 10
result:
ok vertex cover of size 5
Test #5:
score: 0
Accepted
time: 1ms
memory: 3660kb
input:
10 20 1 9 3 6 3 7 8 9 3 8 1 4 5 10 7 10 4 6 7 9 9 10 2 7 1 6 5 8 2 9 1 7 5 7 3 10 2 6 4 10
output:
6 1 6 7 8 9 10
result:
ok vertex cover of size 6
Test #6:
score: 0
Accepted
time: 1ms
memory: 3700kb
input:
50 100 29 49 1 43 12 49 31 46 6 42 25 29 27 37 2 39 3 43 34 43 4 38 2 40 9 14 7 20 22 31 9 42 3 31 36 49 23 33 17 18 34 47 20 36 11 24 5 17 6 29 21 22 5 41 19 28 31 37 8 47 8 42 8 28 1 48 31 41 6 32 14 36 32 42 27 47 1 40 6 30 26 49 9 44 12 22 30 46 9 11 11 28 18 32 13 15 17 44 16 29 17 42 4 21 17 2...
output:
not smol
result:
ok not smol
Test #7:
score: 0
Accepted
time: 0ms
memory: 3768kb
input:
50 300 18 29 25 33 13 27 22 38 43 50 9 47 36 43 15 33 33 36 23 39 17 46 28 35 40 49 24 26 15 30 39 43 9 48 2 4 7 20 13 21 35 40 2 46 12 22 17 33 9 49 17 32 15 28 24 32 7 38 12 32 18 37 13 30 4 24 5 22 6 17 4 26 3 13 5 29 27 34 1 12 16 22 3 14 1 21 22 27 20 49 9 34 18 36 40 42 21 33 44 45 2 49 13 37 ...
output:
not smol
result:
ok not smol
Test #8:
score: 0
Accepted
time: 1ms
memory: 3828kb
input:
50 1000 3 35 32 34 2 24 3 10 15 34 9 45 16 24 7 10 15 39 38 40 17 45 21 35 18 36 15 50 22 29 34 40 3 36 43 50 17 19 7 30 27 44 12 48 9 18 14 20 16 30 1 34 20 35 19 33 2 27 13 20 19 32 38 48 27 37 4 28 5 45 6 43 1 36 9 13 4 18 14 32 10 38 3 44 8 47 6 41 18 38 13 40 18 28 40 47 15 18 42 48 15 47 31 36...
output:
not smol
result:
ok not smol
Test #9:
score: 0
Accepted
time: 2ms
memory: 3720kb
input:
200 300 64 134 92 154 82 142 33 198 26 185 24 74 32 144 26 118 113 122 98 130 74 84 70 184 45 181 44 136 44 134 67 185 77 160 21 50 80 181 62 78 196 199 37 174 91 105 17 74 158 166 26 172 70 129 128 133 152 173 15 86 37 67 55 91 45 74 60 141 179 184 22 168 65 161 62 67 117 152 174 181 35 99 80 103 3...
output:
not smol
result:
ok not smol
Test #10:
score: 0
Accepted
time: 3ms
memory: 3800kb
input:
200 1000 19 159 64 180 15 88 82 136 22 57 92 200 86 87 176 194 57 106 116 179 101 128 27 137 41 71 35 139 48 153 177 178 80 131 9 156 29 122 101 148 88 163 90 116 16 72 8 166 100 116 97 161 19 143 78 163 23 119 104 146 91 161 52 66 183 196 29 123 84 86 41 109 65 76 82 161 138 182 108 156 35 94 101 1...
output:
not smol
result:
ok not smol
Test #11:
score: 0
Accepted
time: 12ms
memory: 4056kb
input:
200 5000 60 81 22 145 156 181 27 44 49 89 69 176 61 64 16 199 46 50 75 103 26 168 6 35 60 75 51 117 41 105 20 154 69 100 75 195 22 115 67 72 170 190 31 115 10 200 51 129 14 147 161 163 9 72 22 113 70 87 112 184 28 81 178 197 72 180 171 192 71 116 71 174 30 95 20 157 50 125 142 184 18 130 82 110 65 1...
output:
not smol
result:
ok not smol
Test #12:
score: 0
Accepted
time: 1ms
memory: 3728kb
input:
500 300 201 309 17 37 39 176 416 493 86 475 163 215 127 283 122 274 107 412 7 93 294 434 335 360 50 87 364 372 55 192 341 411 236 299 286 349 79 208 137 470 141 421 21 324 4 165 232 473 367 397 400 475 30 77 177 435 116 133 115 281 416 482 198 498 300 410 173 457 176 450 157 179 402 425 219 486 39 3...
output:
147 6 12 22 30 34 37 39 44 50 52 55 64 66 67 76 78 86 92 93 94 96 101 107 109 110 118 120 121 133 140 148 151 159 160 165 166 170 176 177 179 181 182 183 185 187 188 196 198 201 202 208 211 215 219 221 231 233 242 245 247 251 258 259 260 264 268 271 274 278 280 281 283 285 290 294 298 299 301 303 30...
result:
ok vertex cover of size 147
Test #13:
score: 0
Accepted
time: 10ms
memory: 3764kb
input:
500 1000 232 237 263 478 147 353 131 318 45 109 218 452 377 436 61 326 29 372 394 484 72 374 312 449 451 461 26 113 25 188 21 282 453 484 261 295 449 489 225 422 125 168 123 449 23 211 251 484 40 185 38 304 6 337 71 142 287 356 315 413 185 411 68 111 453 457 70 187 20 183 107 361 324 466 277 483 10 ...
output:
not smol
result:
ok not smol
Test #14:
score: 0
Accepted
time: 25ms
memory: 4288kb
input:
500 5000 78 84 13 468 95 135 258 447 258 267 226 321 132 282 238 355 194 248 75 485 325 390 46 182 156 284 272 289 204 361 15 228 322 448 410 430 35 317 227 386 325 398 207 443 36 280 73 153 117 459 396 494 234 430 140 199 49 357 26 128 177 210 15 231 351 379 357 484 299 489 376 454 177 377 228 331 ...
output:
not smol
result:
ok not smol
Test #15:
score: -100
Time Limit Exceeded
input:
500 124750 260 435 24 351 41 342 79 458 63 342 463 485 313 372 88 486 300 435 144 440 88 480 83 373 126 356 129 486 118 416 83 138 439 447 59 222 1 162 367 487 137 286 253 261 255 451 329 461 276 328 66 184 76 441 228 492 93 396 288 420 2 424 257 318 216 342 249 474 152 200 206 485 13 332 353 406 20...