QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#211197 | #5492. Toll Roads | hagry | WA | 224ms | 66724kb | C++14 | 4.1kb | 2023-10-12 11:45:11 | 2023-10-12 11:45:11 |
Judging History
answer
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define MP make_pair
#define all(x) x.begin(),x.end()
#define Hagry ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
const int OO = 1e9 + 5;
const int N = 2e5 + 5;
const int LG = 22;
struct Edge {
int a, b, w;
Edge() {}
Edge(int a, int b, int w) : a(a), b(b), w(w) {}
bool operator<(const Edge &e2) {
return w < e2.w;
}
};
struct Query {
int a, b, w, id;
Query() { w = 0; }
Query(int a, int b, int id) : a(a), b(b), id(id) { w = 0; }
bool operator<(const Query &qu) {
return w < qu.w;
}
};
struct DSU {
int n, comps;
vector<int> sz, par;
DSU(int n) {
this->n = n;
comps = n;
sz.resize(n + 1);
par.resize(n + 1);
for (int i = 1; i <= n; ++i) {
sz[i] = 1;
par[i] = i;
}
}
int find(int x) {
if (par[x] == x) return x;
return find(par[x]);
}
bool same(int a, int b) { return find(a) == find(b); }
bool unite(int a, int b) {
a = find(a), b = find(b);
if (a == b) return false;
if (sz[a] < sz[b]) swap(a, b);
par[b] = a;
sz[a] += sz[b];
comps--;
return true;
}
};
vector<Edge> kruskal(int n, vector<Edge> &edges) {
DSU dsu(n);
sort(all(edges));
vector<Edge> res;
for (auto edge:edges) {
if (dsu.same(edge.a, edge.b))continue;
dsu.unite(edge.a, edge.b);
res.pb(edge);
}
return res;
}
vector<pi> adj[N];
int anc[N][LG], mx[N][LG], d[N];
void dfs(int u, int par, int w) {
anc[u][0] = par;
mx[u][0] = w;
d[u] = d[par] + 1;
for (auto e:adj[u])if(e.F != par)
dfs(e.F, u, e.S);
}
void pre(int n) {
for (int k = 1; k < LG; ++k) {
for (int u = 1; u <= n; ++u) {
anc[u][k] = anc[anc[u][k - 1]][k - 1];
mx[u][k] = max(mx[u][k - 1], mx[anc[u][k - 1]][k - 1]);
}
}
}
pi binLift(int a, int x) {
int mxE = 0;
for (int bt = 0; bt < LG; ++bt) {
if (x & (1 << bt)) {
mxE = max(mxE, mx[a][bt]);
a = anc[a][bt];
}
}
return {a, mxE};
}
int LCA(int a, int b) {
if (d[a] < d[b])
swap(a, b);
int ans = 0;
tie(a, ans) = binLift(a, d[a] - d[b]);
if (a == b)return ans;
for (int bt = LG-1; bt >= 0; --bt) {
if (anc[a][bt] == anc[b][bt])continue;
ans = max({ans, mx[a][bt], mx[b][bt]});
a = anc[a][bt];
b = anc[b][bt];
}
ans = max(ans, mx[a][0]);
return ans;
}
void TC() {
int n, m, q;
cin >> n >> m >> q;
vector<Edge> edges(m);
int a, b, t;
for (int i = 0; i < m; ++i) {
cin >> a >> b >> t;
edges[i] = Edge(a, b, t);
}
vector<Edge> MST = kruskal(n, edges);
for (auto e:MST) {
adj[e.a].pb({e.b, e.w});
adj[e.b].pb({e.a, e.w});
}
dfs(1, 0, 0);
pre(n);
vector<Query> qrys(q);
for (int i = 0; i < q; ++i) {
cin >> a >> b;
qrys[i] = Query(a, b, i);
qrys[i].w = LCA(a, b);
}
DSU dsu(n);
sort(all(edges));
sort(all(qrys));
vector<pi> ans(q);
int ptr = 0;
for (int e = 0; e < m;) {
int st = e;
for (; e < m && edges[st].w == edges[e].w; ++e)
dsu.unite(edges[e].a, edges[e].b);
for (; ptr < q && qrys[ptr].w<=edges[st].w; ++ptr)
ans[qrys[ptr].id] = {qrys[ptr].w, dsu.sz[dsu.find(qrys[ptr].a)]};
}
for(auto e:ans)
cout << e.F << " " << e.S << "\n";
}
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
Hagry
int t = 1;
// cin >> t;
while (t--) {
TC();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 13960kb
input:
4 3 6 1 2 1 2 3 3 3 4 2 1 2 1 3 1 4 2 3 2 4 3 4
output:
1 2 3 4 3 4 3 4 3 4 2 2
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 217ms
memory: 66724kb
input:
200000 199999 200000 40203 148773 165038 148773 54931 77889 54931 9912 192188 9912 180491 24730 180491 77332 36976 77332 43929 146532 43929 43341 172446 43341 141304 121793 141304 105828 148202 105828 72010 107746 72010 152016 156358 152016 150074 115703 150074 117105 73900 117105 57831 59264 57831 ...
output:
165038 3 77889 2 192188 41 24730 2 36976 3 146532 4 172446 20 121793 2 148202 4 107746 2 156358 10 115703 6 73900 5 59264 2 67443 4 26999 2 156979 16 80963 3 56618 2 107615 6 63765 3 19719 2 178439 35 95141 5 72029 4 14650 2 21437 3 109944 6 139220 7 141978 9 102949 2 170997 15 100704 3 75249 2 1312...
result:
ok 200000 lines
Test #3:
score: -100
Wrong Answer
time: 224ms
memory: 66220kb
input:
200000 199999 200000 25566 162429 116811 162429 150239 28436 150239 75366 140258 75366 176680 111452 176680 158813 50710 158813 125248 118834 125248 191706 31210 191706 163115 65321 163115 46167 44831 46167 129128 79156 129128 112971 51160 112971 195397 1773 195397 196884 159329 196884 188278 191759...
output:
116811 3 28436 2 140258 13 118834 10 118834 10 118834 10 191759 21 159329 14 79156 7 159329 14 191759 21 159329 14 159329 14 191759 21 95051 2 197843 41 46441 2 197843 41 197843 41 197843 41 197843 41 179891 15 173651 10 132452 7 179891 15 84295 3 179891 15 173651 10 179891 15 179891 15 179891 15 18...
result:
wrong answer 176374th lines differ - expected: '181203 17', found: '92789 3'