QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#883622 | #6329. Colorful Graph | lzm0107 | ML | 702ms | 44044kb | C++14 | 4.0kb | 2025-02-05 17:29:48 | 2025-02-05 17:29:49 |
Judging History
answer
#include <bits/stdc++.h>
#define lzm0107
using namespace std;
using LL = long long;
using AI2 = array<int, 2>;
using PII = pair<int, int>;
using ULL = unsigned long long;
const int N = 7e3, V = 1.4e4, E = 1e6;
int n, m, s, t;
int a[N + 10], b[N + 10];
vector<int> ev[N + 10], stk;
int dfn[N + 10], low[N + 10], bel[N + 10], idx, tot;
bool ins[N + 10];
int to[E + 10], cap[E + 10], nxt[E + 10], head[V + 10], etot;
int dis[V + 10], gap[V + 10], cur[V + 10];
int fa[N + 10], siz[N + 10], col[N + 10], ctot;
int ind[V + 10];
vector<int> poss[V + 10];
void tarjan(int x){
dfn[x] = low[x] = ++ idx;
stk.push_back(x);
ins[x] = 1;
for(auto i : ev[x]){
if(!dfn[i]){
tarjan(i);
low[x] = min(low[x], low[i]);
}
else if(ins[i]){
low[x] = min(low[x], dfn[i]);
}
}
if(dfn[x] == low[x]){
tot ++ ;
for(int i = stk.back(); ; i = stk.back()){
bel[i] = tot;
ins[i] = 0;
stk.pop_back();
if(i == x){
break;
}
}
}
}
inline void add(int u, int v, int w){
to[etot] = v, cap[etot] = w, nxt[etot] = head[u], head[u] = etot ++ ;
to[etot] = u, nxt[etot] = head[v], head[v] = etot ++ ;
}
int fd(int x){
return x == fa[x] ? x : fa[x] = fd(fa[x]);
}
void un(int u, int v){
u = fd(u), v = fd(v);
if(u != v){
if(siz[u] < siz[v]) swap(u, v);
siz[u] += siz[v], fa[v] = u;
}
}
int SAP(int x, int inf){
if(x == t) return inf;
int ouf = 0;
#define i (cur[x])
for(; ~i; i = nxt[i]){
if(cap[i] && dis[x] == dis[to[i]] + 1){
int tmp = SAP(to[i], min(inf - ouf, cap[i]));
ouf += tmp, cap[i] -= tmp, cap[i ^ 1] += tmp;
if(inf == ouf || dis[s] == t) return ouf;
}
}
if(!( -- gap[dis[x] ++ ])) dis[s] = t;
gap[dis[x]] ++ ;
i = head[x];
#undef i
return ouf;
}
int main(){
ios::sync_with_stdio(false);
*cin.tie(nullptr) << fixed << setprecision(20);
cin >> n >> m;
for(int i = 1; i <= m; i ++ ){
cin >> a[i] >> b[i];
ev[a[i]].push_back(b[i]);
}
for(int i = 1; i <= n; i ++ ){
if(!dfn[i]){
tarjan(i);
}
}
s = tot << 1 | 1, t = s + 1;
memset(head + 1, -1, t << 2);
for(int i = 1; i <= tot; i ++ ){
ev[i].clear();
add(s, i + tot, 1);
add(i, t, 1);
add(i, i + tot, 0x3f3f3f3f);
}
for(int i = 1; i <= m; i ++ ){
ev[bel[a[i]]].push_back(bel[b[i]]);
}
for(int i = 1; i <= tot; i ++ ){
sort(ev[i].begin(), ev[i].end());
ev[i].erase(unique(ev[i].begin(), ev[i].end()), ev[i].end());
for(auto j : ev[i]){
if(i != j){
add(i + tot, j, 0x3f3f3f3f);
}
}
}
memset(dis + 1, 0x3f, t << 2);
dis[t] = 0;
queue<int> q(deque<int>{t});
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = nxt[i]){
if(cap[i ^ 1] && dis[to[i]] == 0x3f3f3f3f){
dis[to[i]] = dis[u] + 1;
q.push(to[i]);
}
}
}
for(int i = 1; i <= t; i ++ ){
if(dis[i] < t){
gap[dis[i]] ++ ;
}
}
memcpy(cur + 1, head + 1, t << 2);
while(dis[s] < t){
SAP(s, 0x3f3f3f3f);
}
iota(fa + 1, fa + tot + 1, 1);
fill(siz + 1, siz + tot + 1, 1);
for(int i = 1; i < s; i ++ ){
for(int j = head[i]; ~j; j = nxt[j]){
if(cap[j] > 0x3f3f0000){
ind[to[j]] ++ ;
}
}
}
for(int i = 1; i < s; i ++ ){
if(!ind[i]){
q.push(i);
}
}
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = nxt[i]){
if(to[i] == s && cap[i]){
poss[u].push_back(u - tot);
}
else if(to[i] == t && !cap[i]){
un(u, poss[u].back());
poss[u].pop_back();
}
}
for(int i = head[u]; ~i; i = nxt[i]){
if(cap[i] > 0x3f3f0000){
assert(cap[i] + cap[i ^ 1] == 0x3f3f3f3f);
assert(poss[u].size() >= cap[i ^ 1]);
for(int j = 1; j <= cap[i ^ 1]; j ++ ){
poss[to[i]].push_back(poss[u].back());
poss[u].pop_back();
}
if(!( -- ind[to[i]])){
q.push(to[i]);
}
}
}
assert(poss[u].empty());
poss[u].clear();
}
for(int i = 1; i <= tot; i ++ ){
if(fa[i] == i){
col[i] = ++ ctot;
}
}
for(int i = 1; i <= n; i ++ ){
cout << col[fd(bel[i])] << ' ';
}
cout << '\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 8428kb
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: 8304kb
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: 1ms
memory: 8360kb
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: 0
Accepted
time: 702ms
memory: 44044kb
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:
165 1622 458 690 1687 1100 1477 1570 1168 1129 1033 912 1440 1374 1445 1592 1339 1671 1054 16 941 294 1177 628 792 183 1571 1705 545 334 864 321 607 1668 1705 492 1587 41 1748 687 1143 820 701 1333 749 1709 220 1132 710 763 512 1622 427 1592 1685 100 1073 808 979 218 313 1537 1670 1532 1474 552 1547...
result:
ok AC
Test #5:
score: 0
Accepted
time: 329ms
memory: 38480kb
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:
2040 2078 2149 233 862 2263 1931 336 1281 2288 2244 1443 1502 2108 1200 1141 1668 2150 2039 2042 696 857 1048 1442 2076 2108 1538 1276 1944 1358 1334 2265 1638 2061 115 478 686 2144 59 68 975 119 594 589 1811 1917 11 1558 771 787 2260 767 1152 1699 906 1971 1153 862 157 1612 1384 2290 1693 2243 403 ...
result:
ok AC
Test #6:
score: 0
Accepted
time: 545ms
memory: 40084kb
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:
790 791 464 319 2153 792 165 1691 1063 789 1663 793 788 1678 1123 529 794 385 1087 992 982 795 787 1631 1289 266 912 413 796 786 1193 797 958 869 1428 798 1559 421 94 785 1889 1784 1100 799 1613 2086 1020 303 800 2253 377 801 959 2110 1001 323 1720 784 583 2324 802 2220 803 2006 1254 1400 221 2203 8...
result:
ok AC
Test #7:
score: -100
Memory Limit Exceeded
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:
1 345 87 1728 250 71 229 2 1397 3 175 1596 1695 1039 404 308 203 1720 879 4 406 531 956 1144 384 247 323 744 5 6 727 7 866 1518 190 317 215 1525 1373 1052 638 1469 872 826 8 298 9 476 10 397 1646 563 11 12 781 13 1495 1042 968 944 14 78 764 1638 959 15 1479 252 1215 8 325 1572 598 16 1734 1350 1564 ...