QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#97647 | #6329. Colorful Graph | yangjl | WA | 4ms | 14892kb | C++20 | 4.5kb | 2023-04-17 18:52:04 | 2023-04-17 18:52:08 |
Judging History
answer
#include<iostream>
#include<cmath>
#include<cstring>
#include<cassert>
#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<vector>
#include<set>
#include<unordered_set>
#include<bitset>
#include<climits>
#include<numeric>
#include<functional>
#include<iomanip>
#include<random>
#ifdef YJL
#include<debug.h>
#else
#define debug(args...)0
#define debug_n(a,n)0
#endif
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
namespace Graph {
struct ty {
int t,l,next;
} edge[int(2e6)+10];
int en,head[N];
void initGraph(int n) {
fill_n(head+1,n,-1),en=0;
}
void addEdge(int x,int y,int z=0) {
edge[en]={y,z,head[x]};
head[x]=en++;
}
}
int n,m;
int dfn[N], low[N], dn;
int sta[N], insta[N], top;
int color[N], cn;
void tarjan(int u) {
using namespace Graph;
dfn[u]=low[u]=++dn;
sta[++top]=u;
insta[u]=1;
for (int i=head[u]; ~i; i=edge[i].next) {
int v=edge[i].t;
if (!dfn[v]) {
tarjan(v);
low[u]=min(low[u], low[v]);
} else if (insta[v])// 返祖边和横插边
low[u]=min(low[u], dfn[v]);
}
if (dfn[u]==low[u]) {
++cn;
for (int v=-1; v!=u;) {
v=sta[top--];
insta[v]=0;
color[v]=cn;
}
}
}
void converge(int n) {
fill_n(dfn+1,n,0), dn=cn=0;
for(int i=1; i<=n; ++i)
if(!dfn[i])
tarjan(i);
}
namespace NetFlow {
using T=int;
const T INF=T(1)<<(sizeof(T)*8-2);
const int _V=N;
const int _E=1000000*2+10;
int s0=_V-1,t0=s0-1;
int head[_V],en;
struct ty {
int t, next;
T l;
} edge[_E];
void initGraph() {
memset(head, -1, sizeof(head)), en=0;
}
void addEdge(int x, int y, T z=0) {
edge[en]= {y, head[x], z};
head[x]=en++;
if(en&1) addEdge(y, x);
}
int dis[_V];
bool bfs(int s,int t) {
memset(dis,-1,sizeof dis);
queue<int> q;
dis[s]=0;
q.push(s);
while(q.size()) {
int u=q.front();
q.pop();
for (int i=head[u]; ~i; i=edge[i].next) {
int v=edge[i].t;
if(dis[v]==-1&&edge[i].l>0) {
dis[v]=dis[u]+1;
q.push(v);
}
}
}
return dis[t]!=-1;
}
T dfs(int u, T flow,int t) {
if(u==t) return flow;
T ans=flow;
for (int i=head[u]; ~i&&ans; i=edge[i].next) {
int v=edge[i].t;
if (dis[v]==dis[u]+1&&edge[i].l>0) {
T tmp=dfs(v, min(edge[i].l, ans), t);
edge[i].l-=tmp;
edge[i^1].l+=tmp;
ans-=tmp;
}
}
if(ans==flow) dis[u]=-1;
return flow-ans;
}
T dinic(int s,int t) {
T ans=0;
while(bfs(s,t)) ans+=dfs(s,INF,t);
return ans;
}
void printGraph() {
auto dot=[&](int u)->string{
if(u==s0) return "S";
if(u==t0) return "T";
return to_string(u);
};
cerr<<endl<<"Graph with "<<en<<" edges:"<<endl;
for(int i=0; i<en; ++i) {
int u=edge[i^1].t, v=edge[i].t;
T w=edge[i].l;
cerr<<dot(u)<<" -> "<<dot(v)<<" : ";
cerr<<(w==INF?"INF":to_string(w))<<endl;
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin>>n>>m;
Graph::initGraph(n);
vector<pair<int,int>> edg;
while(m--) {
int u,v;
cin>>u>>v;
Graph::addEdge(u,v);
edg.emplace_back(u,v);
}
converge(n);
debug_n(color+1,n);
// 缩点之后跑网络流
using namespace NetFlow;
initGraph();
for(int i=1; i<=cn; ++i) {
addEdge(s0,i+cn,1);
addEdge(i,t0,1);
}
for(auto [u,v]:edg) {
int x=color[u];
int y=color[v];
if(x==y) continue;
addEdge(x+cn,y,1);
addEdge(x+cn,y+cn,1);// 向所有能到的点连边
}
int flow=dinic(s0,t0);
vector<int> paint(cn+1);
for(int u=1,c=0; u<=cn; ++u) {
// 找前驱
int flag=0;
for(int i=head[u]; ~i; i=edge[i].next) {
int v=edge[i].t;
auto w=edge[i].l;
if(v!=s0 && (i&1) && w==1) {
flag=1;
break;
}
}
if(flag) continue;
// debug(u);
// 没有前驱,标颜色
paint[u]=++c;
for(int now=u; now;) {
debug(now);
int nxt=0;
for(int i=head[now+cn]; ~i; i=edge[i].next) {
if(i&1) continue;
int v=edge[i].t, w=edge[i].l;
if(w==1) continue;
debug(now,v);
// vis[i]=1;
head[now+cn]=edge[i].next;
if(v>cn) {
nxt=v-cn;
}else {
paint[v]=c;
nxt=v;
}
break;
}
now=nxt;
}
}
for(int i=1; i<=n; ++i)
cout<<paint[color[i]]<<" ";
return 0;
}
/*
*/
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 12296kb
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: 4ms
memory: 12396kb
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: 2ms
memory: 10292kb
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: 2ms
memory: 14892kb
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:
1749 1552 914 1362 2116 1750 1751 989 456 1260 2157 3126 1752 3342 1288 1753 724 1754 360 1710 761 590 3364 1246 1562 1755 1756 2503 1071 1757 580 637 1758 1759 973 974 1170 1760 3388 1761 1298 545 1762 694 1763 388 1764 2439 1765 1766 12 1767 838 3144 1526 1388 2649 1768 295 1769 618 2935 2015 2294...
result:
wrong answer Integer 2116 violates the range [1, 1750]