QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#426458 | #6329. Colorful Graph | ooooxxxx | RE | 2ms | 9364kb | C++14 | 3.4kb | 2024-05-31 12:06:57 | 2024-05-31 12:06:58 |
Judging History
answer
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<stack>
using namespace std;
int fa[100000],col[100000];
int g(int x){
if(x==fa[x])return x;
return fa[x]=g(fa[x]);
}
namespace dinic{
#define int long long
const int inf=1e18;
const int N=2*130*130;
const int M=1e5;
vector<pair<int,int>>v[N];
int cnt=-1;
int s,t,c[N];
void link(int x,int y,int _c){
cnt++;v[x].push_back(make_pair(y,cnt));c[cnt]=_c;
cnt++;v[y].push_back(make_pair(x,cnt));c[cnt]=0;
}int dis[N];
void bfs(){
queue<int>q;
memset(dis,-1,sizeof(dis));
q.push(s);dis[s]=0;
while(!q.empty()){
int x=q.front();q.pop();
for(auto [y,id]:v[x]){
if((!c[id])||dis[y]!=-1) continue;
dis[y]=dis[x]+1;q.push(y);
}
}
}
int cur[N];
int dfs(int x,int lim){
if(x==t)return lim;
if(!lim)return 0;
int ans=0;
for(int &i=cur[x];i<v[x].size();i++){
auto[y,id]=v[x][i];
if(c[id]==0||dis[y]!=dis[x]+1) continue;
int res=dfs(y,min(lim,c[id]));
c[id]-=res,c[id^1]+=res,ans+=res,lim-=res;
if(!lim) break;
}return ans;
}
int dinic(){
int ans=0;
while(1){
memset(cur,0,sizeof(cur));bfs();
int res=dfs(s,inf);
if(!res) break;
ans+=res;
}return ans;
}
int dfs(int x){
for(auto[y,id]:v[x]){
if(c[id^1]==0||id%2==1) continue;
c[id^1]--;c[id]++;
if(y==t) return x;
return dfs(y);
}
}
void solve(int now){
for(auto[y,id]:v[s]){
if(c[id]==0){
int u=y,v=dfs(y)-now;
// cerr<<"? "<<u<<" "<<v<<endl;
fa[g(v)]=g(u);
}
}
}
}
bool vis[100100];
int low[100100];
int dfn[100100];
int belong[100100];
int siz[100100];
int now, step, sum;
vector<int> v[100100];
stack<int> s;
void tarjan(int x)
{
if (vis[x])return;
low[x] = dfn[x] = ++step;
vis[x] = true;
s.push(x);
for (int i = 0; i < v[x].size(); i++)
{
int y = v[x][i];
if (!dfn[y])
{
tarjan(y);
low[x] = low[x] < low[y] ? low[x] : low[y];
}
if (vis[y])
{
low[x] = low[x] < dfn[y] ? low[x] : dfn[y];
}
}
if (low[x] == dfn[x])
{
int y = -1;
now++;
vis[x] = false;
while (y != x)
{
y = s.top();
s.pop();
belong[y] = now;
siz[now]++;
vis[y] = false;
}
}
}
using dinic::link;
int32_t main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++){
int x,y;cin>>x>>y;
v[x].push_back(y);
}
for(int i=1;i<=n;i++)
if(!dfn[i]) tarjan(i);
dinic::s=now*2+1;
dinic::t=now*2+2;
for(int i=1;i<=n;i++){
for(int j:v[i]){
if(belong[i]==belong[j]){
continue;
}
int u=belong[i],v=belong[j];
link(u,v+now,1000000000);
}
}
for(int i=1;i<=now;i++) link(dinic::s,i,1);
for(int i=1;i<=now;i++) link(now+i,dinic::t,1);
for(int i=1;i<=now;i++) link(now+i,i,1000000000);
cerr<<"?? "<<now-dinic::dinic()<<" "<<now<<endl;
for(int i=1;i<=now;i++) fa[i]=i;
dinic::solve(now);
int id=0;
for(int i=1;i<=now;i++){
if(g(i)==i) col[i]=(++id);
}
for(int i=1;i<=n;i++) cout<<col[g(belong[i])]<<" ";
// for(auto[y,id]:dinic::v[dinic])
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 9240kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
1 2 2 1 1
result:
ok AC
Test #2:
score: 0
Accepted
time: 2ms
memory: 9364kb
input:
5 7 1 2 2 1 4 3 5 1 5 4 4 1 4 5
output:
1 1 2 2 2
result:
ok AC
Test #3:
score: 0
Accepted
time: 2ms
memory: 8800kb
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
Runtime Error
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 ...