QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#43127 | #4513. Slide Parade | xiaoyaowudi | 35 ✓ | 7717ms | 48768kb | C++14 | 3.8kb | 2022-08-08 11:31:59 | 2022-08-08 11:32:01 |
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;
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;
}
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;
// if(ti==18){
// printf("%d %d %d\n",eds[i][0],eds[i][1],c);
// }
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();
// if(ti==18){
printf("%d\n",l);
for(int i(0);i<l;++i)
printf("%d%c",ap[i]," \n"[i==l-1]);
// }
clear();
}
return 0;
}
详细
Test #1:
score: 11
Accepted
time: 7717ms
memory: 48540kb
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: 4415ms
memory: 48768kb
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)