QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#215282 | #6329. Colorful Graph | pengpeng_fudan | WA | 1036ms | 6720kb | C++14 | 3.3kb | 2023-10-15 08:06:42 | 2023-10-15 08:06:42 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ull=unsigned long long;
using ll=long long;
const int N=7010,M=4*7010;
vector<int> ve[7010];
vector<int> re[7010];
int n,m;
int stk[N],sz[N],scc[N];
int dfn[N]; //时间戳,记录dfs序
int low[N];//追溯值
bool instk[N];
int cnt=0,tot=0,top=0;
void tarjan(int u){
dfn[u]=low[u]=++tot;
stk[++top]=u,instk[u]=true;
for(auto i:ve[u]){
if(!dfn[i]){
tarjan(i);
low[u]=min(low[u],low[i]);
}
else if(instk[i]) low[u]=min(low[u],dfn[i]);
}
if(dfn[u]==low[u]){
++cnt;
int y;
do{
y=stk[top--];
instk[y]=false;
scc[y]=cnt;
sz[cnt]++;
}while(y!=u);
}
}
const ll INF=1e18;
struct edge{int v;ll w,pre_w,nxt;}edge[2*M];
int head[2*N],cur[2*N],dep[2*N];
int ctz=1,st,ed;
int pot_sum;
void add(int u,int v,ll w){
edge[++ctz]={v,w,w,head[u]};
head[u]=ctz;
edge[++ctz]={u,0,0,head[v]};
head[v]=ctz;
}
bool bfs(){
copy(head,head+pot_sum+1,cur);
fill(dep,dep+pot_sum+1,-1);
queue<int> qe;
dep[st]=0;qe.push(st);
while(!qe.empty()){
int t=qe.front();qe.pop();
for(int i=head[t];i;i=edge[i].nxt){
int v=edge[i].v;ll w=edge[i].w;
if(w>0&&dep[v]==-1){
dep[v]=dep[t]+1;
qe.push(v);
}
}
}
return dep[ed]!=-1;
}
ll dfs(int u=st,ll flow=INF){
ll rem=flow;
if(u==ed) return flow;
for(int i=cur[u];i&&flow;i=edge[i].nxt){
cur[u]=i;
int v=edge[i].v;ll w=edge[i].w;
if(w>0&&dep[v]==dep[u]+1){
ll t=dfs(v,min(w,rem));
rem-=t;
edge[i].w-=t;
edge[i^1].w+=t;
}
}
return flow-rem;
}
ll dinic(){
ll ans=0;
while(bfs()) ans+=dfs();
return ans;
}
int col[N];
// int fa[N];
// int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);}
// void merge(int u,int v){
// u=get(u),v=get(v);
// fa[u]=v;
// }
int c=0;
int color(int u){
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].v,w=edge[i].pre_w-edge[i].w;
if(v!=st&&v!=ed&&w>0){
int t=color(v);
edge[i].w++;
if(u>cnt) col[u-cnt]=t;
else col[u]=t;
return t;
}
}
if(u>cnt) u-=cnt;
if(!col[u]) col[u]=++c;
return col[u];
}
void solve() {
cin>>n>>m;
int u,v;
for(int i=1;i<=m;i++){
cin>>u>>v;
ve[u].push_back(v);
}
for(int i=1;i<=n;i++){
if(!dfn[i]) tarjan(i);
}
st=0,ed=2*cnt+1;
for(int i=1;i<=cnt;i++){add(0,i,1),add(i+cnt,ed,1),add(i+cnt,i,INF);}
for(int i=1;i<=n;i++){
for(auto j:ve[i]){
if(scc[j]!=scc[i]) add(scc[i],scc[j]+cnt,INF);
}
}
//cout<<cnt<<'\n';
pot_sum=2*cnt+1;
ll ans=dinic();
// for(int i=1;i<=cnt;i++) fa[i]=i;
for(int i=1;i<=cnt;i++){
if(col[i]) continue;
color(i);
}
for(int i=1;i<=n;i++) cout<<col[scc[i]]<<' ';
cout<<'\n';
}
int main() {
ios::sync_with_stdio(0),cin.tie(0);
int _ = 1;
//cin >> _;
while(_--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3864kb
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: 1ms
memory: 4200kb
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: 1ms
memory: 3868kb
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: 1036ms
memory: 6720kb
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:
447 107 414 203 1 173 97 377 655 1505 1 1 97 1 232 57 516 30 702 25 498 585 1 253 102 29 29 1 334 28 1171 566 28 27 385 385 286 27 1 26 1525 614 26 534 25 687 25 1 24 24 879 23 1292 1 122 187 1 23 1031 22 575 1 1 1 158 22 1 1 243 21 21 710 21 981 20 1179 1 570 932 1 1 1 427 443 1 451 610 751 603 1 3...
result:
wrong answer 13 and 7 are not connected