QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#151651 | #6329. Colorful Graph | Forever_Young# | RE | 1ms | 4332kb | C++14 | 4.0kb | 2023-08-27 13:00:15 | 2023-08-27 13:00:18 |
Judging History
answer
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;++i)
#define per(i,n) for(int i=n;i>=1;--i)
#define pb push_back
#define mp make_pair
#define stack stck
#define N 7010
#define M 7100
#define inf 1000000007
struct EDGE { int adj, next; } edge[M];
int n, m, top, gh[N];
int dfn[N], low[N], cnt, ind, stop, instack[N], stack[N], belong[N];
void addedge(int x, int y) {
edge[++top].adj = y;
edge[top].next = gh[x];
gh[x] = top;
}
void tarjan(int x) {
dfn[x] = low[x] = ++ind;
instack[x] = 1; stack[++stop] = x;
for (int p=gh[x]; p; p=edge[p].next)
if (!dfn[edge[p].adj]) {
tarjan(edge[p].adj);
low[x] = min(low[x], low[edge[p].adj]);
} else if (instack[edge[p].adj]) {
low[x] = min(low[x], dfn[edge[p].adj]);
}
if (dfn[x] == low[x]) {
++cnt; int tmp=0;
while (tmp!=x) {
tmp = stack[stop--];
belong[tmp] = cnt;
instack[tmp] = 0;
}
}
}
pair<int,int> save[M];
set<pair<int,int> > dic; //duplicate edges
namespace flow
{
struct EDGE { int adj, w, cap, next; } edge[10*M];
int n, top, gh[2*N], nrl[2*N] , S, T;
void addedge(int x, int y, int w) {
//cout<<"fuck: "<<x<<" "<<y<<" "<<w<<endl;
edge[++top].adj = y;
edge[top].w = w;
edge[top].cap=w;
edge[top].next = gh[x];
gh[x] = top;
edge[++top].adj = x;
edge[top].w = 0;
edge[top].cap=0;
edge[top].next = gh[y];
gh[y] = top;
}
int dist[2*N], q[2*N];
int bfs() {
memset(dist, 0, sizeof(dist));
q[1] = S; int head = 0, tail = 1; dist[S] = 1;
while (head != tail) {
int x = q[++head];
for (int p=gh[x]; p; p=edge[p].next)
if (edge[p].w && !dist[edge[p].adj]) {
dist[edge[p].adj] = dist[x] + 1;
q[++tail] = edge[p].adj;
}
}
return dist[T];
}
int dinic(int x, int delta) {
if (x==T) return delta;
for (int& p=nrl[x]; p && delta; p=edge[p].next)
if (edge[p].w && dist[x]+1 == dist[edge[p].adj]) {
int dd = dinic(edge[p].adj, min(delta, edge[p].w));
if (!dd) continue;
edge[p].w -= dd;
edge[p^1].w += dd;
return dd;
}
return 0;
}
int work() {
int ans = 0;
while (bfs()) {
memcpy(nrl, gh, sizeof(gh));
int t; while (t = dinic(S, 2147483647)) ans += t;
}
return ans;
}
}
int col[N],nxt[N],du[N];
int flowedge[M];
vector<pair<int,int> > G[2*N];
vector<int> le,ri;
void dfs(int x)
{
while (!G[x].empty())
{
int i=G[x].size()-1;
G[x][i].second--;
int y=G[x][i].first;
if (G[x][i].second==0)G[x].pop_back();
if (x!=flow::S && x!=flow::T)
if (y!=flow::S && y!=flow::T)
le.pb(x),ri.pb(y);
dfs(y);
return;
}
}
int main()
{
cin>>n>>m;
rep(i,m)
{
scanf("%d%d",&save[i].first,&save[i].second);
addedge(save[i].first,save[i].second);
}
//SCC
rep(i,n)if (!dfn[i])tarjan(i);
//Flow
flow::top=1;
flow::S=0;
flow::T=2*cnt+1;
rep(i,cnt)
{
flow::addedge(flow::S,i,1);
flow::addedge(cnt+i,i,inf);
flow::addedge(cnt+i,flow::T,1);
}
dic.clear();
rep(i,m)flowedge[i]=-1;
rep(i,m)
if (belong[save[i].first]!=belong[save[i].second])
{
int x=belong[save[i].first];
int y=belong[save[i].second];
if (dic.find(mp(x,y))!=dic.end())continue;
dic.insert(mp(x,y));
flow::addedge(x,cnt+y,inf);
flowedge[i]=flow::top;
}
int f=flow::work();
for(int i=flow::S;i<=flow::T;i++)
for(int p=flow::gh[i];p;p=flow::edge[p].next)
if (flow::edge[p].cap)
{
if (flow::edge[p].cap==flow::edge[p].w)continue;
G[i].pb(mp(flow::edge[p].adj,flow::edge[p].cap-flow::edge[p].w));
}
//for(int i=flow::S;i<=flow::T;i++)
// for(auto p:G[i])cout<<i<<" "<<p.first<<" "<<p.second<<endl;
rep(i,f)dfs(flow::S);
for(int i=0;i<le.size();i++)nxt[le[i]]=ri[i]-cnt,du[ri[i]-cnt]++;
assert(le.size()==f);
int cur=0;
rep(i,cnt)
if (!du[i])
{
++cur;
int p=i;
while (p!=0)col[p]=cur,p=nxt[p];
}
rep(i,n)printf("%d ",col[belong[i]]);
puts("");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4280kb
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: 0ms
memory: 4268kb
input:
5 7 1 2 2 1 4 3 5 1 5 4 4 1 4 5
output:
2 2 1 2 2
result:
ok AC
Test #3:
score: 0
Accepted
time: 1ms
memory: 4332kb
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
Dangerous Syscalls
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 ...