QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#43122 | #4513. Slide Parade | xiaoyaowudi | 0 | 7185ms | 27668kb | C++14 | 4.1kb | 2022-08-08 11:05:26 | 2022-08-08 11:05:26 |
Judging History
answer
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
constexpr int N=210,M=5010,inf=1e9;
vector<int> es[N];
int eds[M][3],n,m,ind[N],otd[N];
bool vis[N];
void clear()
{
memset(vis,0,sizeof(vis));
memset(ind,0,sizeof(ind));
memset(otd,0,sizeof(otd));
for(int i(1);i<=n;++i)
es[i].clear();
}
void dfs(int u){
vis[u]=true;
for(int v:es[u])
if(!vis[v])
dfs(v);
}
namespace flow
{
constexpr int N=410,M=5010+(N<<1);
int hd[N],nxt[M<<1],cur[N],tv[M<<1],ecnt=1,tag[N],w[M<<1];
void clear()
{
ecnt=1;
memset(hd,0,sizeof(hd));
}
int add_edge(int u,int v,int c)
{
nxt[++ecnt]=hd[u];
tv[ecnt]=v;
hd[u]=ecnt;
w[ecnt]=c;
return ecnt;
}
int bfs(int s,int t)
{
memset(tag,0,sizeof(tag));
static int qq[N];int l(1),r(1);
qq[r++]=s;tag[s]=1;
while(l<r)
{
int u=qq[l++];
for(int p(hd[u]);p;p=nxt[p]) if(w[p] && !tag[tv[p]])
{
tag[tv[p]]=tag[u]+1;
qq[r++]=tv[p];
}
}
return tag[t]!=0;
}
int dfs(int u,int t,int fl)
{
if(u==t) return fl;
int ret(0);
for(int (&p)(cur[u]);p;p=nxt[p])
if(w[p] && tag[tv[p]]==tag[u]+1)
{
int k=dfs(tv[p],t,min(w[p],fl));
fl-=k;ret+=k;
w[p]-=k;w[p^1]+=k;
if(!fl)
break;
}
return ret;
}
int dinic(int s,int t)
{
int ans(0);
while(bfs(s,t))
{
memcpy(cur,hd,sizeof(cur));
ans+=dfs(s,t,inf);
}
return ans;
}
}
namespace euler_cycle
{
constexpr int N=1000010;
int hd[N],nxt[N],tv[N],ecnt=1,mk[N];bool vis[N];
void clear()
{
ecnt=1;
memset(hd,0,sizeof(hd));
}
void add_edge(int u,int v)
{
// printf("u %d v %d\n",u,v);
nxt[++ecnt]=hd[u];
tv[ecnt]=v;
hd[u]=ecnt;
}
void dfs(int u)
{
// printf("u %d mk[u] %d\n",u,mk[u]);
vis[u]=true;
for(int p(hd[u]);p;p=nxt[p])
if(!vis[tv[p]])
{
mk[tv[p]]=p^1;
dfs(tv[p]);
}
}
void dfs(int u,vector<int> &ans)
{
// printf("dfs %d\n",u);
if(hd[u]==mk[u])
hd[u]=nxt[hd[u]];
if(!hd[u] && mk[u])
{
ans.push_back(tv[mk[u]]);
dfs(tv[mk[u]],ans);
}
else if(hd[u])
{
int v=tv[hd[u]];hd[u]=nxt[hd[u]];
ans.push_back(v);
dfs(v,ans);
}
}
vector<int> calc(int s=1)
{
memset(vis,0,sizeof(vis));
memset(mk,0,sizeof(mk));
dfs(s);
vector<int> ret;
dfs(s,ret);
return ret;
}
}
int calc(int k)
{
int s(n*2+1),t(s+1);
flow::clear();
int tot(0);
for(int i(1);i<=n;++i)
{
flow::add_edge(s,i,k-otd[i]);
flow::add_edge(i,s,0);
flow::add_edge(i+n,t,k-ind[i]);
flow::add_edge(t,i+n,0);
tot+=(k-ind[i]);
}
for(int i(1);i<=m;++i)
{
flow::add_edge(eds[i][0],eds[i][1]+n,inf);
eds[i][2]=flow::add_edge(eds[i][1]+n,eds[i][0],0);
}
return flow::dinic(s,t)-tot;
}
bool check(int mid)
{
return calc(mid)>=calc(mid-1);
}
int main()
{
// freopen("GC.in","r",stdin);
// freopen("GC.out","w",stdout);
int TC;
scanf("%d",&TC);
for(int ti(1);ti<=TC;++ti)
{
printf("Case #%d: ",ti);
scanf("%d%d",&n,&m);
for(int i(1),u,v;i<=m;++i)
{
scanf("%d%d",&u,&v);
es[u].push_back(v);
++ind[v];++otd[u];
eds[i][0]=u;eds[i][1]=v;
}
dfs(1);
bool ok(true);
for(int i(1);i<=n && ok;++i)
if(!vis[i])
ok=false;
if(!ok)
{
printf("IMPOSSIBLE\n");
clear();
continue;
}
// int bl(0),br(1000000/n);
// for(int i(1);i<=n;++i)
// bl=max(bl,max(ind[i],otd[i]));
// int ans(bl),mid;
// ++bl;
// while(bl<=br){
// mid=(bl+br)>>1;
// if(check(mid))
// {
// ans=mid;
// bl=mid+1;
// }else
// br=mid-1;
// }
int ans(1000000/n);
int k(calc(ans));
// printf("%d %d\n",ans,k);
if(k!=0)
{
printf("IMPOSSIBLE\n");
clear();
continue;
}
euler_cycle::clear();
for(int i(1);i<=m;++i)
{
int c=flow::w[eds[i][2]]+1;
for(int j(1);j<=c;++j)
euler_cycle::add_edge(eds[i][0],eds[i][1]);
}
auto ap=euler_cycle::calc();
int l=ap.size();
printf("%d\n1 ",l+1);
for(int i(0);i<l;++i)
printf("%d%c",ap[i]," \n"[i==l-1]);
clear();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 7185ms
memory: 27584kb
input:
100 5 8 1 2 1 3 1 4 1 5 2 1 3 1 4 1 5 1 5 10 1 3 1 4 2 3 2 5 3 1 3 4 3 5 4 2 5 1 5 3 5 10 1 4 2 3 2 5 3 1 3 5 4 2 4 3 4 5 5 1 5 2 3 6 1 2 1 3 2 1 2 3 3 1 3 2 5 10 1 2 1 5 2 3 2 4 3 1 4 3 4 5 5 2 5 3 5 4 4 10 1 2 1 3 1 4 2 1 2 3 2 4 3 1 3 4 4 2 4 3 5 10 1 2 1 3 2 1 2 4 3 1 3 5 4 2 4 5 5 3 5 4 5 10 1 ...
output:
Case #1: IMPOSSIBLE Case #2: 1000001 1 4 2 5 3 5 3 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1 4 2 5 3 1...
result:
wrong answer the slide from 5 to 2 hasn't be used (test case 18)
Test #2:
score: 0
Wrong Answer
time: 4044ms
memory: 27668kb
input:
100 199 4980 1 95 1 96 1 105 1 124 1 130 1 131 1 135 1 137 1 139 1 140 1 141 1 147 1 155 1 172 1 174 1 179 1 183 1 188 1 193 1 196 1 197 2 3 2 5 2 13 2 14 2 17 2 20 2 24 2 26 2 30 2 41 2 44 2 45 2 52 2 56 2 67 2 70 2 71 2 74 2 78 2 84 2 85 2 90 2 92 2 93 2 97 2 107 2 111 2 113 2 122 2 124 2 128 2 13...
output:
Case #1: IMPOSSIBLE Case #2: IMPOSSIBLE Case #3: 999995 1 193 195 197 200 169 196 195 197 200 169 191 195 197 200 169 178 196 195 197 200 169 174 195 197 200 169 164 198 193 194 182 197 200 169 164 193 194 182 197 200 169 164 174 194 182 197 200 169 164 162 198 193 194 182 197 200 169 164 162 197 20...
result:
wrong answer the slide from 7 to 4 hasn't be used (test case 3)