QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#180951 | #6329. Colorful Graph | ucup-team228# | WA | 4ms | 5068kb | C++20 | 5.0kb | 2023-09-16 14:37:51 | 2023-09-16 14:37:52 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct DSU {
static const int N = 7e3 + 10;
int Parent[N], Rank[N], Size[N], cnt;
void init(int n) {
for (int i = 1; i <= n; i++) {
Parent[i] = i, Rank[i] = 0, Size[i] = 1;
}
cnt = n;
}
int find(int v) {
if (Parent[v] == v) return v;
else return Parent[v] = find(Parent[v]);
}
void unite(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
if (Rank[u] < Rank[v]) swap(u, v);
if (Rank[v] == Rank[u]) Rank[u]++;
Parent[v] = u, cnt--;
Size[u] += Size[v];
}
};
struct HopcroftKarp {
static const int N = 7e3 + 10;
vector<int> adj_left[N];
int matchL[N] = {};
int matchR[N] = {};
int root[N], prev[N], qq[N];
void add_edge(int u, int v) {
adj_left[u].push_back(v);
}
void init(int L, int R) {
for (int i = 1; i <= L; i++) {
adj_left[i].clear();
matchL[i] = 0;
}
for (int i = 1; i <= R; i++) {
matchR[i] = 0;
}
}
void init_matching(int L, int R) {
for (int i = 1; i <= L; i++) {
matchL[i] = 0;
}
for (int i = 1; i <= R; i++) {
matchR[i] = 0;
}
}
int maxmatching(int L, int R) {
int sz = 0;
for (bool updated = true; updated; ) {
updated = false;
for (int i = 1; i <= L; i++) {
root[i] = -1;
prev[i] = -1;
qq[i] = 0;
}
int qi = 0, qj = 0;
for (int i = 1; i <= L; i++) {
if (matchL[i] == 0) {
qq[qj++] = i, root[i] = i, prev[i] = i;
}
}
while (qi < qj) {
int u = qq[qi++];
if (matchL[root[u]] != 0) {
continue;
}
for (int v : adj_left[u]) {
if (matchR[v] == 0) {
while (v != 0) {
matchR[v] = u, swap(matchL[u], v), u = prev[u];
}
updated = true;
sz++;
break;
}
if (prev[matchR[v]] == -1) {
v = matchR[v], prev[v] = u, root[v] = root[u], qq[qj++] = v;
}
}
}
}
return sz;
}
};
DSU dsu;
HopcroftKarp hk;
const int N = 7e3 + 10;
vector<int> g[2][N];
bool used[N];
vector<int> post;
vector<vector<int>> comps;
int id[N];
int col_comp[N];
int ans[N];
void add_edge(int u, int v) {
g[0][u].push_back(v);
g[1][v].push_back(u);
}
void dfs_inv(int v) {
used[v] = true;
for (int to : g[1][v]) {
if (!used[to]) {
dfs_inv(to);
}
}
post.push_back(v);
}
void dfs(int v, vector<int>& comp) {
used[v] = true;
comp.push_back(v);
for (int to : g[0][v]) {
if (!used[to]) {
dfs(to, comp);
}
}
}
void Kosaraju(int n) {
for (int i = 1; i <= n; i++) {
used[i] = false;
}
for (int i = 1; i <= n; i++) {
if (!used[i]) {
dfs_inv(i);
}
}
reverse(post.begin(), post.end());
for (int i = 1; i <= n; i++) {
used[i] = false;
}
for (int v : post) {
if (!used[v]) {
vector<int> comp;
dfs(v, comp);
comps.push_back(comp);
}
}
reverse(comps.begin(), comps.end());
for (int i = 0; i < comps.size(); i++) {
for (int v : comps[i]) {
id[v] = i + 1;
}
}
// edges go from left to right
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
add_edge(u, v);
}
Kosaraju(n);
for (int i = 1; i <= n; i++) {
for (int j : g[0][i]) {
hk.add_edge(id[i], id[j]);
}
}
hk.maxmatching(comps.size(), comps.size());
dsu.init(comps.size());
for (int i = 1; i <= comps.size(); i++) {
if (hk.matchL[i]) {
dsu.unite(i, hk.matchL[i]);
}
}
for (int i = 1, j = 0; i <= comps.size(); i++) {
if (dsu.find(i) == i) {
col_comp[i] = ++j;
}
}
for (int i = 1; i <= comps.size(); i++) {
col_comp[i] = col_comp[dsu.find(i)];
for (int v : comps[i - 1]) {
ans[v] = col_comp[i];
}
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
cout << "\n";
#ifdef LOCAL
cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4084kb
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: 4060kb
input:
5 7 1 2 2 1 4 3 5 1 5 4 4 1 4 5
output:
2 2 1 1 1
result:
ok AC
Test #3:
score: 0
Accepted
time: 0ms
memory: 4144kb
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: 4ms
memory: 5068kb
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 1856 2163 1952 612 1194 1006 2126 2404 2405 150 44 1024 119 1981 948 2265 816 2452 1774 2247 2334 287 2002 1851 170 958 812 2083 1728 2453 2315 1507 898 2134 2134 2035 1335 222 1445 2454 2363 286 2283 1400 2437 1632 242 1428 320 2631 925 2632 709 1871 1936 183 1343 2633 114 2324 646 706 421 1907 1...
result:
wrong answer Integer 1856 violates the range [1, 1750]