QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#328301 | #1844. Cactus | HBYoutze | WA | 256ms | 47792kb | C++14 | 3.5kb | 2024-02-15 18:59:32 | 2024-02-15 18:59:32 |
Judging History
answer
#include <bits/stdc++.h>
//#define int long long
//#define mod 1000000007
using namespace std;
int n,m,head[300010],cnt=1,ru[300010],tmp,cz[600010],tot,ctt[300010];
int huan[300010],ruh[300010],head2[300010],cnt2,tmp2,zhan[300010],ztop;
bool vis[300010],vis2[300010],vis3[300010],vis4[1000010];
vector<int> h[300010],d[300010];
set<int> hu[300010];
struct edge
{
int next,to;
}a[1000010],a2[1000010];
void ad(int u,int v)
{
a[++cnt].to=v;a[cnt].next=head[u];head[u]=cnt;ru[v]++;
a[++cnt].to=u;a[cnt].next=head[v];head[v]=cnt;ru[u]++;
}
void ad2(int u,int v)
{
a2[++cnt2].to=v;a2[cnt2].next=head2[u];head2[u]=cnt2;
a2[++cnt2].to=u;a2[cnt2].next=head2[v];head2[v]=cnt2;
}
void dfs(int x,int fa)
{
if(vis2[x])
{
int now=ztop;tmp2++;
h[tmp2].push_back(x);huan[tmp2]++;
for(auto j:d[x])
{
ad2(tmp2,j);
if(!hu[j].count(x))
{
hu[j].insert(x);ruh[j]++;
}
}
if(!d[x].empty())
{
hu[tmp2].insert(x);ruh[tmp2]++;
}
d[x].push_back(tmp2);ctt[x]++;
while(zhan[now]!=x)
{
h[tmp2].push_back(zhan[now]);
for(auto j:d[zhan[now]])
{
ad2(tmp2,j);
if(!hu[j].count(zhan[now]))
{
hu[j].insert(zhan[now]);ruh[j]++;
}
}
if(!d[zhan[now]].empty())
{
hu[tmp2].insert(zhan[now]);ruh[tmp2]++;
}
d[zhan[now]].push_back(tmp2);ctt[zhan[now]]++;
now--;huan[tmp2]++;
}
return;
}
vis2[x]=1;zhan[++ztop]=x;
for(int i=head[x];i;i=a[i].next)
{
int v=a[i].to;
if(vis[v]||v==fa||vis4[i]||vis4[i^1]) continue;
vis4[i]=1;dfs(v,x);
}
ztop--;
}
signed main()
{
cin>>n>>m;tmp=n;
for(int i=1;i<=m;i++)
{
int u,v;cin>>u>>v;ad(u,v);
}
set<int> echo;cout<<"0 ";
for(int i=1;i<=n;i++)
{
if(ru[i]%2) echo.insert(i);
}
while(!echo.empty())
{
set<int>::iterator it=echo.begin();
int now=*it;echo.erase(now);
cz[++tot]=now;vis[now]=1;tmp--;
for(int i=head[now];i;i=a[i].next)
{
int v=a[i].to;
if(vis[v]) continue;
ru[v]--;
if(ru[v]%2) echo.insert(v);
else echo.erase(v);
if(!ru[v])
{
tmp--;vis[v]=1;
}
}
}
queue<int> q;
if(tmp==0) goto Marshall_Maximizer;
cz[++tot]=0;
for(int i=1;i<=n;i++)
{
if(!vis[i]&&!vis2[i]) dfs(i,0);
}
for(int i=1;i<=tmp2;i++)
{
if(ruh[i]==1||!ruh[i]) q.push(i);
}
while(!q.empty())
{
int u=q.front();q.pop();
if(vis3[u]) continue;
int k,fl=0,fl2=0;
if(ruh[u]) k=*hu[u].begin();
else k=h[u][0];
for(auto j:h[u])
{
if(j==k)
{
fl2=fl;
if(huan[u]%2==0) fl^=1;
continue;
}
cz[++tot]=j+fl*n;
fl^=1;
}
int lst,nxt;
for(int i=0;i<h[u].size();i++)
{
if(h[u][i]!=k) continue;
if(i==0) lst=h[u][h[u].size()-1];
else lst=h[u][i-1];
if(i==h[u].size()-1) nxt=h[u][0];
else nxt=h[u][i+1];
break;
}
if(huan[u]%2)
{
if(fl2)
{
cz[++tot]=lst+n;cz[++tot]=nxt;
}
else
{
cz[++tot]=lst;cz[++tot]=nxt+n;
}
}
else
{
if(fl2)
{
cz[++tot]=lst+n;cz[++tot]=nxt+n;
}
else
{
cz[++tot]=lst;cz[++tot]=nxt;
}
}
vis3[u]=1;
if(!ruh[u]) cz[++tot]=k;
ctt[k]--;
if(ctt[k]>1) continue;
for(int i=head2[u];i;i=a2[i].next)
{
int v=a2[i].to;
if(vis3[v]) continue;
ruh[v]--;
if(ruh[v]==1||!ruh[v]) q.push(v);
if(ruh[v]) hu[v].erase(k);
}
}
Marshall_Maximizer:
cout<<tot<<'\n';
for(int i=1;i<=tot;i++)
{
if(cz[i]==0) cout<<"2\n";
else cout<<"1 "<<cz[i]<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 31776kb
input:
3 3 1 2 1 3 2 3
output:
0 6 2 1 2 1 6 1 3 1 5 1 1
result:
ok You are right!
Test #2:
score: 0
Accepted
time: 0ms
memory: 31732kb
input:
7 7 1 2 1 3 2 3 2 4 2 5 3 6 3 7
output:
0 5 1 4 1 2 1 1 1 6 1 3
result:
ok You are right!
Test #3:
score: -100
Wrong Answer
time: 256ms
memory: 47792kb
input:
300000 368742 1 143504 1 234282 2 91276 2 296320 3 274816 4 212293 4 258214 5 253489 5 295826 6 96521 6 252745 6 267103 6 269879 7 5293 7 295586 8 44304 8 57067 8 233291 9 190526 10 18682 11 7440 12 24695 12 172561 12 243692 12 280316 13 80152 13 268749 14 146394 14 207280 15 151280 15 226848 16 458...
output:
0 259887 1 3 1 8 1 9 1 10 1 11 1 16 1 20 1 21 1 25 1 28 1 29 1 30 1 32 1 33 1 34 1 41 1 42 1 43 1 44 1 47 1 48 1 53 1 54 1 55 1 57 1 59 1 62 1 65 1 73 1 75 1 77 1 80 1 81 1 87 1 88 1 94 1 95 1 101 1 102 1 103 1 106 1 112 1 123 1 128 1 129 1 133 1 136 1 137 1 138 1 140 1 141 1 143 1 145 1 146 1 150 1...
result:
wrong answer The number of remaining edges is not equal to m'.