QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#623303 | #6329. Colorful Graph | Carlos_S48 | WA | 5ms | 6400kb | C++20 | 4.6kb | 2024-10-09 11:13:34 | 2024-10-09 11:13:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define fastIO ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define ft first
#define sd second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define read(x) for(auto &el : x) cin >> el;
#define print(x) for(auto &el : x) {cout << el << " ";} cout << endl;
#define printR(x, ini, n) for(int i = ini; i < n; i++) { if(i != ini) {cout << " ";}; cout << x[i];}; cout << endl;
typedef long long ll;
typedef pair<int,int> pii;
// scanf("%d %d %d %d",&w,&b,&d,&s); %lld
// printf("%d\n" ,points);
// const double PI = acos(-1);
inline int add(int a, int b, const int &mod) { return a+b >= mod ? a+b-mod : a+b; }
inline int sbt(int a, int b, const int &mod) { return a-b < 0 ? a-b+mod : a-b; }
inline int mul(int a, int b, const int &mod) { return 1ll*a*b % mod; }
struct edge { int v, cap, inv, flow; bool rev; };
struct network {
int n, s, t;
vector<int> lvl;
vector<vector<edge>> g;
network(int n) : n(n), lvl(n), g(n) {}
void add_edge(int u, int v, int c) {
g[u].pb({v, c, (int)g[v].size(), 0, false});
g[v].pb({u, 0, (int)g[u].size()-1, c, true});
}
bool bfs() {
fill(lvl.begin(), lvl.end(), -1);
queue<int> q;
lvl[s] = 0;
for (q.push(s); q.size(); q.pop()) {
int u = q.front();
for (auto &e : g[u]) {
if (e.cap > 0 && lvl[e.v] == -1) {
lvl[e.v] = lvl[u]+1;
q.push(e.v);
}
}
}
return lvl[t] != -1;
}
int dfs(int u, int nf) {
if (u == t) return nf;
int res = 0;
for (auto &e : g[u]) {
if (e.cap > 0 && lvl[e.v] == lvl[u]+1) {
int tf = dfs(e.v, min(nf, e.cap));
res += tf; nf -= tf; e.cap -= tf;
g[e.v][e.inv].cap += tf;
g[e.v][e.inv].flow -= tf;
e.flow += tf;
if (nf == 0) return res;
}
}
if (!res) lvl[u] = -1;
return res;
}
int max_flow(int so, int si, int res = 0) {
s = so; t = si;
while (bfs()) res += dfs(s, INT_MAX);
return res;
}
};
const int inf = 1e9;
const int MX = 7e3+5;
vector<int> g[MX];
set<int> dag[MX];
stack<int> st;
int low[MX], pre[MX], cnt;
int degree[MX], color[MX];
int comp[MX], SCC;
int n, m;
void tarjan(int u) {
low[u] = pre[u] = cnt++;
st.push(u);
for (auto &v : g[u]) {
if (pre[v] == -1) tarjan(v);
low[u] = min(low[u], low[v]);
}
if (low[u] == pre[u]) {
SCC++;
while (true) {
int v = st.top(); st.pop();
low[v] = inf;
comp[v] = SCC;
if (u == v) break;
}
}
}
void init() {
cnt = SCC = 0;
for (int i = 0; i <= n; i++) {
g[i].clear();
pre[i] = -1;
}
}
int main() {
fastIO;
cin >> n >> m;
init();
for (int i = 0, u, v; i < m; ++i) {
cin >> u >> v;
g[u].pb(v);
}
for (int i = 1; i <= n; ++i) {
if (pre[i] == -1) {
tarjan(i);
}
}
for (int u = 1; u <= n; ++u) {
for (int &v : g[u]) {
if (comp[u] != comp[v]) {
dag[comp[u]].insert(comp[v]);
}
}
}
network nf(2 * (SCC + 1));
for (int u = 1; u <= SCC; ++u) {
nf.add_edge(0, u, 1);
nf.add_edge(u + SCC, 2 * SCC + 1, 1);
nf.add_edge(u + SCC, u, inf);
for (auto &v : dag[u]) {
nf.add_edge(u, v + SCC, 1);
}
}
int mx_f = nf.max_flow(0, 2 * SCC + 1);
// cout << SCC - mx_f << endl;
for (int u = 1; u <= SCC; ++u) {
for (auto &v : dag[u]) {
degree[v]++;
}
}
queue<int> q;
for (int u = 1; u <= SCC; ++u) {
if (!degree[u]) {
q.push(u);
}
}
int u, x, c = 0;
while (!q.empty()) {
u = q.front();
q.pop();
if (!color[u]) {
++c;
x = u;
while (!color[x]) {
color[x] = c;
for (auto &[v, cap, inv, flow, rev] : nf.g[x]) {
if (flow && !rev) {
x = v - SCC;
break;
}
}
}
}
for (auto &v : dag[u]) {
degree[v]--;
if (!degree[v]) {
q.push(v);
}
}
}
for (int i = 1; i <= n; ++i) {
if (i > 1) cout << " ";
cout << color[comp[i]];
}
cout << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4136kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
2 1 1 2 2
result:
ok AC
Test #2:
score: 0
Accepted
time: 1ms
memory: 3964kb
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: 4216kb
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: 6400kb
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:
1 1949 2587 2137 3 2 3 2510 3041 2241 1 1 4 1 2211 5 2775 6 3139 1789 2740 2909 3 2253 1939 7 8 6 2424 9 2921 2867 10 11 2524 2524 2327 12 3 13 2203 2954 14 2804 15 3111 16 3 17 18 3483 19 2664 3 1973 2110 1 20 3208 21 2881 3 3 3 2053 22 3 3 2236 23 24 3157 25 3309 26 2902 6 2874 3414 3 3 3 2607 263...
result:
wrong answer Integer 1949 violates the range [1, 1750]