QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#426381 | #6329. Colorful Graph | qawszx | WA | 544ms | 11068kb | C++20 | 3.7kb | 2024-05-31 09:37:21 | 2024-05-31 09:37:22 |
Judging History
answer
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<ctime>
#include<random>
#include<array>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n'
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n'
#define DE(fmt,...) fprintf(stderr, "Line %d : " fmt "\n",__LINE__,##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
r=0;bool w=0;char ch=getchar();
while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
const int N=7010;
const int M=1000010;
const int inf=0x7fffffff;
const ll INF=0x7fffffffffffffff;
int tot,S,T;
int head[M],cur[M],dis[M],ent=1;
struct Edge{
int nxt,to,fl;
}e[M<<1];
inline void adde(int x,int y,int w){
e[++ent]={head[x],y,w};head[x]=ent;
e[++ent]={head[y],x,0};head[y]=ent;
}
int bfs(){
for(int i=1;i<=tot;i++)dis[i]=inf,cur[i]=head[i];
dis[S]=0;queue<int>q;q.push(S);
while(!q.empty()){
int x=q.front();q.pop();
for(int i=head[x];i;i=e[i].nxt){
int v=e[i].to,w=e[i].fl;
if(w){
if(dis[v]>dis[x]+1){
dis[v]=dis[x]+1;
q.push(v);
}
}
}
}
return dis[T]!=inf;
}
ll dfs(int x,ll flow){
if(x==T)return flow;
ll flo=0;
for(int i=cur[x];i&&flo<flow;i=e[i].nxt){
cur[x]=i;
int v=e[i].to;ll w=e[i].fl;
if(dis[v]==dis[x]+1&&w){
ll fl=dfs(v,min(flow-flo,w));
flo+=fl;e[i].fl-=fl;
e[i^1].fl+=fl;
}
}
return flo;
}
ll dinic(){
ll mxfl=0;
while(bfs()){
mxfl+=dfs(S,INF);
}
return mxfl;
}
int n,m;
int dfn[N],low[N],st[N],top,col[N],sct,dft;
vector<int>eg[N];
void tarjan(int x){
dfn[x]=low[x]=++dft;st[++top]=x;
for(auto v:eg[x]){
if(!dfn[v]){
tarjan(v);
cmin(low[x],low[v]);
}
else if(!col[v])cmin(low[x],dfn[v]);
}
if(dfn[x]==low[x]){
++sct;
while(1){
int t=st[top--];
col[t]=sct;
if(t==x)break;
}
}
}
int fa[N],a[N];
int okl[N],okr[N];
int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}
void getans(int x,int t){
for(int i=head[x];i;i=e[i].nxt){
if(i%2==0){
if(e[i].fl){
int v=e[i].to;
if(v>n&&v<=2*n){
if(okr[v-n]){
fa[getfa(t)]=getfa(v-n);
okr[v-n]=0;
return ;
}
}
if(v>=1&&v<=2*n){
e[i].fl--;
getans(v,t);
return ;
}
}
}
}
}
int main(){
read(n);read(m);
for(int i=1;i<=m;i++){
int u,v;read(u);read(v);
eg[u].pb(v);
}
for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
int t=n;
n=sct;
S=2*n+1;T=tot=S+1;
for(int i=1;i<=t;i++)for(auto j:eg[i])
if(col[i]!=col[j])
adde(col[i],col[j]+n,inf);
for(int i=1;i<=n;i++)adde(S,i,1),adde(i+n,T,1),adde(i+n,i,inf);
// cout<<n-dinic()<<'\n';
dinic();
for(int i=1;i<=n;i++)fa[i]=i;
for(int i=head[S];i;i=e[i].nxt){
if(!e[i].fl){
okl[e[i].to]=1;
}
}
for(int i=head[T];i;i=e[i].nxt){
if(e[i].fl){
okr[e[i].to-n]=1;
}
}
for(int i=1;i<=n;i++)
if(okl[i])
getans(i,i);
int cnt=0;
for(int i=1;i<=n;i++)if(getfa(i)==i)a[i]=++cnt;
for(int i=1;i<=t;i++){
cout<<a[getfa(col[i])]<<' ';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 7764kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
2 2 2 1 2
result:
ok AC
Test #2:
score: 0
Accepted
time: 2ms
memory: 9660kb
input:
5 7 1 2 2 1 4 3 5 1 5 4 4 1 4 5
output:
1 1 2 1 1
result:
ok AC
Test #3:
score: 0
Accepted
time: 0ms
memory: 7944kb
input:
8 6 6 1 3 4 3 6 2 3 4 1 6 4
output:
1 1 1 1 2 1 3 4
result:
ok AC
Test #4:
score: -100
Wrong Answer
time: 544ms
memory: 11068kb
input:
7000 6999 4365 4296 2980 3141 6820 4995 4781 24 2416 5844 2940 2675 3293 2163 3853 5356 262 6706 1985 1497 5241 3803 353 1624 5838 4708 5452 3019 2029 6161 3849 4219 1095 1453 4268 4567 1184 1857 2911 3977 1662 2751 6353 6496 2002 6628 1407 4623 425 1331 4445 4277 1259 3165 4994 1044 2756 5788 5496 ...
output:
1750 1657 1326 1564 1751 1752 1751 1365 1110 1505 1750 1750 1753 1750 1519 1754 516 1755 1061 25 1257 585 1751 1498 1662 1756 1757 1755 1404 1758 1171 1198 1759 1760 1357 1358 286 1761 1751 1762 1525 1158 1763 534 1764 687 1765 1751 1766 1767 890 1768 1292 1751 1647 187 1750 1769 1031 1770 1192 1751...
result:
wrong answer Integer 1751 violates the range [1, 1750]