QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#421285 | #961. Smol Vertex Cover | CarroT1212 | WA | 1ms | 4068kb | C++14 | 3.0kb | 2024-05-25 16:08:58 | 2024-05-25 16:08:59 |
Judging History
answer
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
using namespace std; bool MEM;
using ll=long long; using ld=long double;
using pii=pair<int,int>; using pll=pair<ll,ll>;
const int I=1e9;
const ll J=1e18,N=507;
ll n,m,a[N],b[N],mch[N],vis[N],ans;
ld st;
vector<ll> e[N];
vector<pll> v;
mt19937 dnr(time(0));
bool hun(ll p) {
shuffle(e[p].begin(),e[p].end(),dnr);
vis[p]=1;
for (ll i:e[p]) {
ll j=mch[i];
if (!j) return vis[i]=1,mch[p]=i,mch[i]=p,1;
if (vis[j]) continue;
mch[p]=i,mch[i]=p,mch[j]=0;
if (hun(j)) return 1;
mch[i]=j,mch[j]=i,mch[p]=0;
}
return 0;
}
void fnd() {
memset(mch,0,sizeof(mch)),v.clear();
ll flg=1;
while ((clock()-st)/CLOCKS_PER_SEC<(ld)n*n*n/(500*500*500)*0.8&&flg) {
flg=ans;
for (ll i=1;i<=n;i++) if (!mch[i]) fill(vis+1,vis+n+1,0),ans+=hun(i);
flg=ans-flg;
}
for (ll i=1;i<=n;i++) if (mch[i]>i) v.pb({i,mch[i]});
}
namespace T {
const ll N=507;
ll n,ans[N];
ll dfn[N<<1],low[N<<1],co[N<<1],cnn,cno;
vector<ll> e[N<<1];
stack<ll> st;
void ini(ll _n) {
n=_n,cnn=cno=0;
for (ll i=1;i<=n*2;i++) dfn[i]=co[i]=0;
}
void clr() { for (ll i=1;i<=n*2;i++) e[i].clear(); }
void dda(ll x,ll a,ll y,ll b) { e[x+n*!a].pb(y+n*b); }
void led(ll x,ll a,ll y,ll b) { e[x+n*!a].pop_back(); }
void add(ll x,ll a,ll y,ll b) { dda(x,a,y,b),dda(y,b,x,a); }
void del(ll x,ll a,ll y,ll b) { led(x,a,y,b),led(y,b,x,a); }
void tar(ll p) {
dfn[p]=low[p]=++cnn,st.push(p);
for (ll i:e[p]) {
if (!dfn[i]) tar(i),low[p]=min(low[p],low[i]);
else if (!co[i]) low[p]=min(low[p],dfn[i]);
}
if (dfn[p]==low[p]) {
cno++;
while (st.top()!=p) co[st.top()]=cno,st.pop();
co[p]=cno,st.pop();
}
}
bool sat() {
for (ll i=1;i<=n*2;i++) if (!dfn[i]) tar(i);
for (ll i=1;i<=n;i++) {
if (co[i]==co[i+n]) return 0;
ans[i]=!(co[i]<co[i+n]);
}
return 1;
}
}
bool solve() {
if (T::ini(n),T::sat()) {
vector<ll> ans;
for (ll i=1;i<=n;i++) if (T::ans[i]) ans.pb(i);
cout<<ans.size()<<"\n";
for (ll i:ans) cout<<i<<" "; cout<<"\n";
return 1;
}
return 0;
}
void mian() {
scanf("%lld%lld",&n,&m);
for (ll i=1,x,y;i<=m;i++) scanf("%lld%lld",&x,&y),e[x].pb(y),e[y].pb(x),a[i]=x,b[i]=y;
st=clock();
fnd();
T::clr(),T::ini(n);
for (ll i=1;i<=m;i++) if (mch[a[i]]!=b[i]) T::add(a[i],1,b[i],1);
for (pll i:v) T::add(i.fi,1,i.se,1);
// solve();
for (ll i=1;i<=n;i++) if (!mch[i]) T::dda(i,0,i,0);
for (pll i:v) T::add(i.fi,0,i.se,0);
if (solve()) return;
for (ll i=1;i<=n;i++) {
if (!mch[i]) T::led(i,0,i,0);
else T::del(i,0,mch[i],0);
T::dda(i,1,i,1);
if (solve()) return;
T::led(i,1,i,1);
if (!mch[i]) T::dda(i,0,i,0);
else T::add(i,0,mch[i],0);
}
cout<<"not smol\n";
}
bool ORY; int main() {
// while (1)
// int t; for (scanf("%d",&t);t--;)
mian();
cerr<<"\n"<<abs(&MEM-&ORY)/1048576<<"MB "<<clock()<<"ms";
return 0;
}
/*
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
*/
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3916kb
input:
5 5 1 2 2 3 3 4 4 5 1 5
output:
3 1 2 4
result:
ok vertex cover of size 3
Test #2:
score: 0
Accepted
time: 1ms
memory: 3800kb
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: 3892kb
input:
3 0
output:
0
result:
ok vertex cover of size 0
Test #4:
score: 0
Accepted
time: 1ms
memory: 4056kb
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: 3828kb
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: 4068kb
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: 1ms
memory: 3792kb
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: -100
Wrong Answer
time: 1ms
memory: 3972kb
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:
25 10 14 15 18 20 22 23 24 27 28 29 31 32 33 34 35 36 37 39 40 41 43 45 47 49
result:
wrong answer not a vertex cover