QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#43128 | #4513. Slide Parade | xiaoyaowudi | 35 ✓ | 1595ms | 59056kb | C++14 | 4.3kb | 2022-08-08 11:38:18 | 2022-08-08 11:38:19 |
Judging History
answer
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
namespace fr{
#include <ctype.h>
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#define putchar(c) (((O==obuf+(1<<23))?(fwrite(obuf,1,O-obuf,stdout),O=obuf):O),*O++=(c))
void rd(char *x){char c=getchar();while(isspace(c)) c=getchar();while(!isspace(c) && c!=EOF) *(x++)=c,c=getchar();*(x++)='\0';}
template<class T> void rd(T &x){T ret=0;bool flg=false;char c=getchar();while(!isdigit(c) && c!='-') c=getchar();
if(c=='-') c=getchar(),flg=true;while(isdigit(c)) ret=(ret<<1)+(ret<<3)+(c-'0'),c=getchar();if(flg) x=-ret;else x=ret;}
void pc(char c){putchar(c);}void flush(){fwrite(obuf,1,O-obuf,stdout),O=obuf;}
template<class T> void wr(T x){if(x<0) putchar('-'),x=-x;if(x==0) putchar('0');static int cnt,stk[110];cnt=0;while(x) stk[++cnt]=x%10,x/=10;for(int i=cnt;i;--i) putchar(stk[i]+'0');}
}
using fr::rd;using fr::pc;using fr::wr;using fr::flush;
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;
void clear()
{
ecnt=0;
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,vector<int> &ans)
{
for(int (&p)(hd[u]);p;)
{
int v=tv[p];
p=nxt[p];
dfs(v,ans);
}
ans.push_back(u);
}
vector<int> calc(int s=1)
{
vector<int> ret;
dfs(s,ret);
reverse(ret.begin(),ret.end());
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;
}
int main()
{
int TC;
rd(TC);
for(int ti(1);ti<=TC;++ti)
{
pc('C');pc('a');pc('s');pc('e');pc(' ');pc('#');wr(ti);pc(':');pc(' ');
rd(n);rd(m);
for(int i(1),u,v;i<=m;++i)
{
rd(u);rd(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)
{
for(char c:"IMPOSSIBLE\n")
if(c!='\0')
pc(c);
clear();
continue;
}
int ans(1000000/n);
int k(calc(ans));
if(k!=0)
{
for(char c:"IMPOSSIBLE\n")
if(c!='\0')
pc(c);
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();
wr(l);pc('\n');
for(int i(0);i<l;++i)
wr(ap[i]),pc(" \n"[i==l-1]);
clear();
}
flush();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 11
Accepted
time: 1595ms
memory: 56928kb
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:
ok (100 test cases)
Test #2:
score: 24
Accepted
time: 1211ms
memory: 59056kb
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: 1000001 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 2...
result:
ok (100 test cases)