QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#91146 | #6127. Kawa Exam | hos_lyric | AC ✓ | 655ms | 25836kb | C++14 | 5.7kb | 2023-03-27 14:54:49 | 2023-03-27 14:54:53 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
int N, M;
vector<int> C;
vector<int> A, B;
vector<vector<int>> G, H;
int zeit;
vector<int> dis, low, sz;
void dfsLow(int u, int pi) {
dis[u] = low[u] = zeit++;
sz[u] = 1;
for (const int i : G[u]) if (pi != i) {
const int v = A[i] ^ B[i] ^ u;
if (~dis[v]) {
chmin(low[u], dis[v]);
} else {
H[u].push_back(i);
dfsLow(v, i);
sz[u] += sz[v];
chmin(low[u], low[v]);
}
}
}
int ZEIT;
vector<int> DIS, FIN;
vector<int> US;
void dfsHLD(int u) {
DIS[u] = ZEIT++;
US.push_back(u);
const int deg = H[u].size();
if (deg > 0) {
const int i0 = H[u][0];
int vm = A[i0] ^ B[i0] ^ u;
int jm = 0;
for (int j = 1; j < deg; ++j) {
const int i = H[u][j];
const int v = A[i] ^ B[i] ^ u;
if (sz[vm] < sz[v]) {
vm = v;
jm = j;
}
}
swap(H[u][0], H[u][jm]);
for (int j = 0; j < deg; ++j) {
const int i = H[u][j];
const int v = A[i] ^ B[i] ^ u;
dfsHLD(v);
}
}
FIN[u] = ZEIT;
}
struct Solver {
int mx;
vector<int> fs, gs;
Solver() {}
Solver(int fsLen, int gsLen) : mx(0), fs(fsLen, 0), gs(gsLen, 0) {
gs[0] = fsLen;
}
void add(int c) {
--gs[fs[c]];
++fs[c];
++gs[fs[c]];
chmax(mx, fs[c]);
}
void rem(int c) {
--gs[fs[c]];
--fs[c];
++gs[fs[c]];
if (!gs[mx]) --mx;
}
};
Solver inside, outside;
void add(int u) {
// cerr<<" add u="<<u<<endl;
inside.add(C[u]);
outside.rem(C[u]);
}
void rem(int u) {
// cerr<<" rem u="<<u<<endl;
inside.rem(C[u]);
outside.add(C[u]);
}
// process all edges below u
// after calling, all vertexes below= u are added
vector<int> ans;
void solve(int u) {
// cerr<<"solve "<<u<<endl;
const int deg = H[u].size();
if (deg > 0) {
for (int j = deg; --j >= 0; ) {
const int i = H[u][j];
const int v = A[i] ^ B[i] ^ u;
solve(v);
// cerr<<" ans["<<i<<"] "<<inside.mx<<" + "<<outside.mx<<endl;
if (dis[v] == low[v]) {
ans[i] = inside.mx + outside.mx;
}
if (j) {
for (int h = DIS[v]; h < FIN[v]; ++h) rem(US[h]);
}
}
for (int j = deg; --j >= 1; ) {
const int i = H[u][j];
const int v = A[i] ^ B[i] ^ u;
for (int h = DIS[v]; h < FIN[v]; ++h) add(US[h]);
}
}
add(u);
}
int main() {
for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
scanf("%d%d", &N, &M);
C.resize(N);
for (int u = 0; u < N; ++u) {
scanf("%d", &C[u]);
}
A.resize(M);
B.resize(M);
for (int i = 0; i < M; ++i) {
scanf("%d%d", &A[i], &B[i]);
--A[i];
--B[i];
}
auto cs = C;
sort(cs.begin(), cs.end());
cs.erase(unique(cs.begin(), cs.end()), cs.end());
const int csLen = cs.size();
for (int u = 0; u < N; ++u) {
C[u] = lower_bound(cs.begin(), cs.end(), C[u]) - cs.begin();
}
// cerr<<"C = "<<C<<endl;
inside = Solver(csLen, N + 1);
outside = Solver(csLen, N + 1);
G.assign(N, {});
H.assign(N, {});
for (int i = 0; i < M; ++i) {
G[A[i]].push_back(i);
G[B[i]].push_back(i);
}
zeit = 0;
dis.assign(N, -1);
low.assign(N, -1);
sz.assign(N, -1);
ZEIT = 0;
DIS.assign(N, -1);
FIN.assign(N, -1);
US.clear();
int sumOrig = 0;
ans.assign(M, 0);
for (int r = 0; r < N; ++r) if (!~dis[r]) {
dfsLow(r, -1);
dfsHLD(r);
// cerr<<"DO ";pv(US.begin()+DIS[r],US.begin()+FIN[r]);
for (int h = DIS[r]; h < FIN[r]; ++h) {
const int u = US[h];
// cerr<<" outside.add u="<<u<<endl;
outside.add(C[u]);
}
solve(r);
const int orig = inside.mx;
// cerr<<" orig = "<<orig<<endl;
sumOrig += orig;
for (int h = DIS[r]; h < FIN[r]; ++h) {
const int u = US[h];
// cerr<<" inside.rem u="<<u<<endl;
inside.rem(C[u]);
for (const int i : H[u]) {
const int v = A[i] ^ B[i] ^ u;
if (dis[v] == low[v]) {
ans[i] -= orig;
}
}
}
}
// cerr<<"H = "<<H<<endl;
// cerr<<"DIS = "<<DIS<<endl;
// cerr<<"FIN = "<<FIN<<endl;
// cerr<<"US = "<<US<<endl;
for (int i = 0; i < M; ++i) {
ans[i] += sumOrig;
}
for (int i = 0; i < M; ++i) {
if (i) printf(" ");
printf("%d", ans[i]);
}
puts("");
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 3620kb
input:
3 7 5 1 2 1 2 1 2 1 1 2 1 3 2 4 5 6 5 7 3 3 1 2 3 1 2 1 3 2 3 2 3 12345 54321 1 2 1 2 1 1
output:
6 5 5 5 4 1 1 1 1 1 1
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 655ms
memory: 25836kb
input:
5557 2 7 79960 79960 2 2 1 1 1 1 2 2 1 1 2 1 1 2 9 8 21881 70740 70740 21881 22458 22458 639 21881 70740 3 3 1 6 5 8 7 5 5 7 2 3 5 1 7 6 6 7 13064 20716 6746 13064 6746 69225 5 5 4 1 4 1 1 6 4 5 3 2 3 2 8 4 45146 14400 45146 45146 14400 72969 14400 45146 8 6 1 3 4 6 8 3 18 13 48132 37949 92338 92338...
output:
2 2 2 2 2 2 2 6 6 7 6 6 6 6 6 3 3 3 4 4 3 3 7 7 7 7 9 9 9 8 9 8 9 8 9 9 10 9 9 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 9 10 9 16 16 16 16 16 17 16 16 10 10 11 10 12 11 10 10 10 10 10 10 10 12 10 10 10 10 10 11 10 9 9 9 9 9 9 9 9 9 9 9 9 9 10 ...
result:
ok 5557 lines
Test #3:
score: 0
Accepted
time: 463ms
memory: 22912kb
input:
10 100000 99999 3983 3983 20157 97983 20157 20157 3983 3983 97983 20157 20157 3983 97983 20157 3983 20157 20157 3983 3983 3983 97983 97983 20157 3983 3983 97983 20157 97983 20157 97983 3983 97983 97983 3983 20157 3983 20157 20157 97983 3983 3983 3983 3983 97983 97983 3983 97983 97983 3983 20157 3983...
output:
33392 33393 33393 33393 33393 33392 33392 33393 33393 33393 33392 33393 33393 33392 33393 33393 33392 33392 33392 33393 33393 33393 33392 33392 33393 33393 33393 33393 33393 33392 33393 33393 33392 33393 33392 33393 33393 33393 33392 33392 33392 33392 33393 33393 33392 33393 33393 33392 33393 33392 ...
result:
ok 10 lines
Test #4:
score: 0
Accepted
time: 441ms
memory: 23424kb
input:
10 100000 99999 27534 27534 3780 3780 27534 53544 27534 3780 3780 53544 53544 27534 53544 53544 3780 3780 3780 3780 53544 27534 3780 3780 53544 27534 27534 53544 27534 27534 53544 27534 27534 27534 3780 27534 27534 3780 3780 3780 27534 53544 3780 53544 27534 3780 3780 3780 27534 27534 27534 3780 275...
output:
33613 33601 33601 33600 33600 33601 33601 33601 33600 33601 33600 33600 33601 33601 33601 33601 33601 33601 33600 33600 33601 33601 33601 33601 33600 33601 33601 33600 33601 33600 33601 33600 33601 33601 33601 33601 33600 33601 33601 33601 33601 33601 33601 33601 33601 33601 33600 33601 33600 33601 ...
result:
ok 10 lines
Test #5:
score: 0
Accepted
time: 450ms
memory: 18968kb
input:
10 100000 99999 92499 92270 92270 92499 92499 92499 92270 54017 92270 92270 92270 54017 54017 54017 54017 92270 92499 54017 92270 54017 92499 92499 92270 92270 54017 54017 54017 54017 92270 92270 92499 54017 54017 92499 92499 54017 92270 92270 54017 92499 92270 92270 54017 54017 54017 92499 92499 54...
output:
33506 33482 33507 33482 33508 33483 33508 33483 33508 33483 33507 33483 33506 33483 33505 33483 33503 33483 33503 33482 33504 33483 33505 33483 33504 33483 33502 33483 33501 33483 33500 33482 33502 33483 33500 33483 33501 33482 33502 33483 33501 33483 33500 33482 33500 33483 33498 33483 33499 33483 ...
result:
ok 10 lines
Test #6:
score: 0
Accepted
time: 457ms
memory: 18968kb
input:
10 100000 99999 76207 76207 88551 88551 98176 76207 98176 88551 88551 98176 88551 76207 76207 98176 98176 76207 76207 88551 76207 88551 76207 88551 88551 76207 88551 76207 98176 88551 76207 98176 88551 88551 76207 88551 98176 88551 76207 76207 98176 88551 76207 98176 76207 88551 88551 88551 88551 76...
output:
33484 33484 33476 33484 33477 33485 33476 33485 33477 33485 33477 33486 33477 33484 33477 33485 33476 33485 33476 33485 33476 33483 33477 33483 33477 33485 33476 33485 33477 33485 33476 33487 33476 33487 33476 33486 33477 33486 33476 33486 33477 33486 33476 33486 33476 33486 33477 33487 33477 33487 ...
result:
ok 10 lines
Test #7:
score: 0
Accepted
time: 480ms
memory: 18116kb
input:
10 100000 99999 70486 49904 70486 49904 87935 49904 49904 87935 87935 49904 49904 87935 49904 87935 87935 70486 49904 87935 87935 49904 70486 87935 49904 70486 87935 87935 49904 49904 49904 87935 70486 70486 70486 49904 70486 87935 87935 87935 70486 87935 70486 49904 87935 49904 49904 87935 70486 87...
output:
33491 33486 33489 33486 33489 33486 33489 33486 33487 33486 33487 33486 33486 33485 33486 33486 33486 33486 33485 33486 33485 33485 33486 33486 33485 33486 33485 33486 33485 33485 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33485 33486 33485 ...
result:
ok 10 lines
Test #8:
score: 0
Accepted
time: 463ms
memory: 18044kb
input:
10 100000 99999 98004 33580 98004 98004 98004 92291 92291 98004 98004 92291 92291 33580 98004 92291 33580 98004 98004 33580 98004 92291 92291 33580 92291 92291 98004 33580 98004 33580 33580 98004 33580 92291 33580 33580 92291 92291 92291 98004 33580 98004 92291 92291 33580 92291 98004 98004 92291 92...
output:
33462 33463 33421 33463 33422 33465 33421 33463 33422 33464 33422 33462 33422 33464 33421 33464 33422 33464 33422 33465 33422 33463 33422 33462 33422 33463 33422 33465 33421 33464 33422 33464 33422 33463 33422 33463 33421 33463 33421 33462 33422 33460 33422 33461 33421 33461 33422 33460 33422 33459 ...
result:
ok 10 lines