QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#105861 | #6329. Colorful Graph | Sommohito | TL | 210ms | 11556kb | C++20 | 6.2kb | 2023-05-15 18:22:46 | 2023-05-15 18:22:49 |
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;
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]);
}
struct bipartite_matching {
int n_left, n_right, flow = 0;
bitset<N>g[N];
std::vector<int> match_from_left, match_from_right;
bipartite_matching(int _n_left, int _n_right)
: n_left(_n_left),
n_right(_n_right),
match_from_left(_n_left, -1),
match_from_right(_n_right, -1),
dist(_n_left) {}
std::vector<int> dist;
void bfs() {
std::queue<int> q;
for (int u = 0; u < n_left; ++u) {
if (!~match_from_left[u])
q.push(u), dist[u] = 0;
else
dist[u] = -1;
}
while (!q.empty()) {
int u = q.front();
q.pop();
for(int v=g[u]._Find_first(); v< g[u].size(); v = g[u]._Find_next(v))
if (~match_from_right[v] && !~dist[match_from_right[v]]) {
dist[match_from_right[v]] = dist[u] + 1;
q.push(match_from_right[v]);
}
}
}
bool dfs(int u) {
for(int v=g[u]._Find_first(); v< g[u].size(); v = g[u]._Find_next(v))
if (!~match_from_right[v]) {
match_from_left[u] = v, match_from_right[v] = u;
return true;
}
for(int v=g[u]._Find_first(); v< g[u].size(); v = g[u]._Find_next(v))
if (dist[match_from_right[v]] == dist[u] + 1 &&
dfs(match_from_right[v])) {
match_from_left[u] = v, match_from_right[v] = u;
return true;
}
return false;
}
int get_max_matching() {
while (true) {
bfs();
int augment = 0;
for (int u = 0; u < n_left; ++u)
if (!~match_from_left[u]) augment += dfs(u);
if (!augment) break;
flow += augment;
}
return flow;
}
std::pair<std::vector<int>, std::vector<int>> minimum_vertex_cover() {
std::vector<int> L, R;
for (int u = 0; u < n_left; ++u) {
if (!~dist[u])
L.push_back(u);
else if (~match_from_left[u])
R.push_back(match_from_left[u]);
}
return {L, R};
}
std::vector<std::pair<int, int>> get_edges() {
std::vector<std::pair<int, int>> ans;
for (int u = 0; u < n_left; ++u)
if (match_from_left[u] != -1)
ans.emplace_back(u, match_from_left[u]);
return ans;
}
};
int head[N];
vector<int>adj[N];
int color[N];
void dfs3(int s, bipartite_matching &bm)
{
debug(s);
used[s] = 1;
for(int i:adj[s])
{
if(!used[i])
{
dfs3(i , bm);
}
bm.g[s] |= bm.g[i];
bm.g[s][i] = 1;
}
bm.g[s][s] = 0;
}
struct DSU
{
vector<int>parent;
vector<int>rnk;
int n;
DSU(int nn)
{
n=nn;
parent.resize(n);
rnk.resize(n);
for(int i=0; i<n; i++)
{
make_set(i);
}
}
void make_set(int x)
{
rnk[x] = 0;
parent[x] = x;
}
int find_set(int x)
{
if(parent[x]!=x)
{
parent[x] = find_set(parent[x]);
}
return parent[x];
}
void Union(int u,int v)
{
int p= find_set(u),q=find_set(v);
if(p==q)
return;
if( rnk[p] > rnk[q])
swap(p,q);
parent[p]= q;
if(rnk[p]==rnk[q])
{
rnk[q] +=1;
}
}
};
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;
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]);
}
}
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();
bipartite_matching bm(id,id);
used.assign(id, 0);
for(int i=0;i<id;i++)
{
if(used[i])
continue;
dfs3(i,bm);
}
bm.get_max_matching();
vector<pii>lol = bm.get_edges();
for(int i=0;i<id;i++)
{
for(int j=0;j<id;j++)
{
if(bm.g[i][j])
{
debug(i,j);
}
}
}
debug(lol);
DSU d(id);
for(auto it:lol)
{
d.Union(it.first, it.second);
}
int bosa = 1;
for(int i=0;i<id;i++)
{
if(d.find_set(i) == i)
{
color[i] = bosa;
bosa++;
}
}
for(int i=0; i<n; i++)
{
cout<<color[d.find_set(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: 1ms
memory: 9792kb
input:
5 5 1 4 2 3 1 3 2 5 5 1
output:
1 1 1 2 1
result:
ok AC
Test #2:
score: 0
Accepted
time: 1ms
memory: 9896kb
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: 9864kb
input:
8 6 6 1 3 4 3 6 2 3 4 1 6 4
output:
4 4 4 4 3 4 2 1
result:
ok AC
Test #4:
score: 0
Accepted
time: 210ms
memory: 10908kb
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 273 1240 554 486 1749 1748 1126 1747 716 1412 1652 1747 1483 673 1746 117 1745 468 1227 1461 1718 1125 736 259 1744 1743 79 1005 1742 1724 1645 1741 1740 1150 1148 633 1739 1258 1738 658 1736 1737 81 1736 1562 1735 1215 1734 1733 1256 1732 1351 295 308 864 1333 1731 611 1730 1666 405 300 852 93...
result:
ok AC
Test #5:
score: 0
Accepted
time: 109ms
memory: 10776kb
input:
7000 6999 4832 1603 5984 6985 5355 3687 6007 2170 5984 3486 3267 2189 538 2123 4343 4553 5855 6168 5984 257 4239 2304 5984 2063 3298 1869 5984 6353 5984 2018 5984 5387 5984 3382 3164 3978 2690 2816 4810 2638 5984 3773 5984 1634 5984 2786 5984 3671 5984 5140 2943 5721 5984 414 1105 4060 3093 796 5984...
output:
580 1004 1692 1840 604 1110 1428 1640 2069 90 176 1753 1639 1043 2228 2333 1311 363 582 47 2332 614 237 1085 1012 445 1465 433 1531 755 661 541 1370 1072 2331 2330 947 1494 2180 2162 2329 2063 1130 2328 483 2327 2273 2326 2325 750 2276 788 70 2095 518 1429 2324 2323 2322 2321 1869 686 2320 178 1508 ...
result:
ok AC
Test #6:
score: 0
Accepted
time: 205ms
memory: 10872kb
input:
7000 6999 1649 5337 1701 3344 4394 2172 3330 39 5932 1141 5381 5340 5453 3300 125 2172 6810 5263 804 2172 6635 2172 676 4740 3015 1183 1710 5769 611 5915 3419 1581 2094 2172 4508 2172 6604 2433 6113 1466 1604 696 1518 1123 1287 2940 4825 2172 5130 4524 2693 2172 106 2172 5157 2172 3693 2172 5198 217...
output:
1992 1991 681 2015 258 1990 2169 1512 1544 1989 1286 1988 1987 471 1448 534 1968 857 1396 1645 1661 1986 1985 516 1271 2068 1771 796 1984 1983 1354 1967 982 1836 670 1982 1050 781 2240 1981 652 811 1486 1966 1005 1365 1601 2031 1980 116 872 1965 1696 118 1630 2011 904 1979 414 14 1964 165 1978 479 1...
result:
ok AC
Test #7:
score: 0
Accepted
time: 139ms
memory: 11000kb
input:
7000 6999 2896 6321 881 2623 5058 2623 4833 2623 4669 2623 4781 5007 1447 2623 4781 4768 4781 3834 2758 4792 797 5055 3784 2623 4781 5510 6606 3040 597 3459 4136 2037 1291 3989 4781 837 4781 4379 5637 2053 1642 2665 4781 4664 4781 952 4924 2511 4781 4201 4781 2352 4781 5362 3901 197 137 2623 2706 19...
output:
1750 1406 1664 23 1501 1680 1522 1749 1397 1748 1576 155 56 712 1347 308 203 31 872 1747 406 1220 956 1144 384 247 323 744 1746 1745 1024 1744 885 233 1561 1434 215 1525 378 1052 1113 282 872 925 1743 1453 1742 1275 1741 1354 105 1188 1740 1739 970 1738 256 709 968 807 1737 1673 987 1638 792 1736 14...
result:
ok AC
Test #8:
score: 0
Accepted
time: 90ms
memory: 10524kb
input:
6999 6998 1269 3969 1269 2429 1269 2609 1269 2515 1269 6166 1269 6614 3108 1269 2105 1269 4670 1269 578 1269 4661 1269 1421 1269 2576 1269 6152 1269 1269 6636 3011 1269 305 1269 5189 1269 1683 1269 6861 1269 1269 5798 1499 1269 282 1269 914 1269 80 1269 677 1269 701 1269 1269 359 6521 1269 1269 1754...
output:
3499 3498 3497 3496 2428 3495 3494 834 2456 256 197 555 3493 3067 3492 3491 3490 2525 3489 3488 3376 3487 3486 809 3485 3484 3483 3482 3481 1826 3480 2434 3479 3478 307 3477 1887 3042 3476 2986 2530 3475 3474 2297 413 3473 3472 3471 3470 3469 979 1949 8 3468 3238 1106 3467 3466 3465 3464 3463 2924 3...
result:
ok AC
Test #9:
score: 0
Accepted
time: 3ms
memory: 10100kb
input:
7000 0
output:
7000 6999 6998 6997 6996 6995 6994 6993 6992 6991 6990 6989 6988 6987 6986 6985 6984 6983 6982 6981 6980 6979 6978 6977 6976 6975 6974 6973 6972 6971 6970 6969 6968 6967 6966 6965 6964 6963 6962 6961 6960 6959 6958 6957 6956 6955 6954 6953 6952 6951 6950 6949 6948 6947 6946 6945 6944 6943 6942 6941 ...
result:
ok AC
Test #10:
score: 0
Accepted
time: 104ms
memory: 11556kb
input:
7000 6999 3138 1903 3285 5919 6182 1430 1164 961 1577 6445 1390 3384 935 5723 6614 6387 4799 2877 3915 5128 5366 5455 2287 3941 2053 2326 4022 6993 488 2922 4327 4701 4674 3221 1666 4773 4356 3232 3888 937 4318 6942 577 1299 4491 1938 5154 1254 790 5532 4286 5478 2918 6725 2853 304 2554 5207 5140 77...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
ok AC
Test #11:
score: -100
Time Limit Exceeded
input:
7000 6999 33 3147 5877 4807 3116 4168 1651 2456 624 1740 6440 3058 6414 489 1023 2523 706 93 5523 598 4211 6063 3570 6840 6566 2971 6614 1907 5893 4389 4022 2527 5096 2345 4682 2134 188 5597 695 4285 1344 3832 3534 879 6574 6252 3759 3444 2167 85 5630 6600 3158 4404 6389 689 4871 6719 4295 6008 3437...