QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#105829 | #6329. Colorful Graph | Sommohito# | WA | 5ms | 5892kb | C++20 | 5.6kb | 2023-05-15 17:11:13 | 2023-05-15 17:11:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#ifdef APURBA
#include "DEBUG_TEMPLATE.h"
#else
#define HERE
#define debug(args...)
#endif
#define ALL(x) x.begin(),x.end()
const int N = 7e3 +5;
typedef pair<int,int> pii;
struct FlowEdge
{
int v, u;
int cap, flow = 0;
FlowEdge(int v, int u, int cap) : v(v), u(u), cap(cap) {}
};
struct Dinic
{
const int flow_inf = 1e6;
vector<FlowEdge> edges;
vector<vector<int> > adj;
int n, m = 0;
int s, t;
vector<int> level, ptr;
queue<int> q;
Dinic(int n, int s, int t) : n(n), s(s), t(t)
{
adj.resize(n);
level.resize(n);
ptr.resize(n);
}
Dinic(int n)
{
this->n = n;
adj.resize(n);
level.resize(n);
ptr.resize(n);
}
void set_src_dest(int s, int t)
{
this->s = s;
this->t= t;
}
void add_edge(int v, int u, int cap)
{
edges.emplace_back(v, u, cap);
edges.emplace_back(u, v, 0);
adj[v].push_back(m);
adj[u].push_back(m + 1);
m += 2;
}
bool bfs()
{
while (!q.empty())
{
int v = q.front();
q.pop();
for (int id : adj[v])
{
if (edges[id].cap - edges[id].flow < 1)
continue;
if (level[edges[id].u] != -1)
continue;
level[edges[id].u] = level[v] + 1;
q.push(edges[id].u);
}
}
return level[t] != -1;
}
int dfs(int v, int pushed)
{
if (pushed == 0)
return 0;
if (v == t)
return pushed;
for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++)
{
int id = adj[v][cid];
int u = edges[id].u;
if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
continue;
int tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
if (tr == 0)
continue;
edges[id].flow += tr;
edges[id ^ 1].flow -= tr;
return tr;
}
return 0;
}
int flow()
{
int f = 0;
while (true)
{
fill(level.begin(), level.end(), -1);
level[s] = 0;
q.push(s);
if (!bfs())
break;
fill(ptr.begin(), ptr.end(), 0);
while (int pushed = dfs(s, flow_inf))
{
f += pushed;
}
}
return f;
}
};
int n,m;
vector<int> g[N], gr[N];
vector<bool> used;
vector<int> order, component;
void dfs1 (int v)
{
used[v] = true;
for (size_t i=0; i<g[v].size(); ++i)
if (!used[ g[v][i] ])
dfs1 (g[v][i]);
order.push_back (v);
}
void dfs2 (int v)
{
used[v] = true;
component.push_back (v);
for (size_t i=0; i<gr[v].size(); ++i)
if (!used[ gr[v][i] ])
dfs2 (gr[v][i]);
}
int head[N];
vector<int>adj[N];
int color[N];
int nxt[N];
void dfs3 (int v)
{
used[v] = true;
if (nxt[v]!=-1 && !used[ nxt[v] ])
dfs3 (nxt[v]);
order.push_back (v);
}
void dfs4 (int v, int c)
{
used[v] = true;
color[v] = c;
if (nxt[v]!=-1 && !used[ nxt[v] ])
dfs4 (nxt[v],c);
}
int32_t main()
{
#ifndef APURBA
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int n,m;
cin>>n>>m;
for(int i=0; i<m; i++)
{
int a, b;
cin>>a>>b;
a--;
b--;
g[a].push_back(b);
gr[b].push_back(a);
}
used.assign (n, false);
for (int i=0; i<n; ++i)
if (!used[i])
dfs1 (i);
used.assign (n, false);
int id = 0;
for (int i=0; i<n; ++i)
{
int v = order[n-1-i];
if (!used[v])
{
dfs2 (v);
for(int j:component)
head[j] = id;
debug(component);
id++;
component.clear();
}
}
for(int i=0; i<n; i++)
{
for(int j:g[i])
{
if(head[i]!=head[j])
adj[head[i]].push_back(head[j]);
}
}
debug(id);
for(int i=0; i<n; i++)
{
sort(adj[i].begin(),adj[i].end());
adj[i].resize(unique(adj[i].begin(),adj[i].end()) - adj[i].begin() );
g[i].clear();
gr[i].clear();
debug(i,adj[i]);
}
used.clear();
order.clear();
Dinic d(id*2 + 2, id*2, id*2+1);
for(int i=0; i<id; i++)
{
for(int j:adj[i])
{
d.add_edge(i,id+j,1);
}
d.add_edge(id*2, i, 1);
d.add_edge(id + i, id*2+1, 1);
}
d.flow();
memset(nxt,-1,sizeof nxt);
for(auto &it:d.edges)
{
if(it.v >=0 && it.v < id && it.u >=id && it.u < 2*id && it.flow == 1)
{
nxt[it.v] = it.u-id;
}
}
used.assign (id, false);
for (int i=0; i<id; ++i)
if (!used[i])
dfs3 (i);
reverse(order.begin(),order.end());
used.assign (id, false);
int bosa = 0;
for (int i:order)
if (!used[i])
{
bosa++;
dfs4(i,bosa);
}
for(int i=0; i<n; i++)
{
// debug(i,head[i]);
cout<<color[head[i]]<<" ";
}
return 0;
}
/*
5 5
1 4
2 3
1 3
2 5
5 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3884kb
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: 3888kb
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: 3860kb
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: 5ms
memory: 5892kb
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 2117 1751 1752 1365 1110 1505 2158 3127 1753 3343 1519 1754 516 1755 1061 25 1257 585 3365 1498 1662 1756 1757 2504 1404 1758 1171 1198 1759 1760 1357 1358 286 1761 3389 1762 1525 1158 1763 534 1764 687 1765 2440 1766 1767 890 1768 1292 3145 1647 187 2650 1769 1031 1770 1192 2936...
result:
wrong answer Integer 2117 violates the range [1, 1750]