QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#308882 | #8136. Rebellious Edge | ucup-team896# | WA | 47ms | 9760kb | C++20 | 4.2kb | 2024-01-20 13:33:20 | 2024-01-20 13:33:21 |
Judging History
answer
// Author: Klay Thompson
// Problem: P4716 【模板】最小树形图
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4716
// Memory Limit: 250 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#ifdef dbg
#define D(...) fprintf(stderr, __VA_ARGS__)
#define DD(...) D(#__VA_ARGS__ " = "), debug_helper::debug(__VA_ARGS__), D("\n")
#include "C:\Users\wsyear\Desktop\OI\templates\debug.hpp"
#else
#define D(...) ((void)0)
#define DD(...) ((void)0)
#endif
#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using namespace std;
const int inf = 1E9;
const int N = 400010;
const int M = 500010;
namespace Edmonds {
struct ltt_node {
int lson, rson;
int val, tag;
int from, to;
int dis;
};
struct leftist_tree {
ltt_node ltt[M];
int tot;
void init() {
rep (i, 1, tot) {
ltt[i].lson = ltt[i].rson = 0;
ltt[i].val = ltt[i].tag = 0;
ltt[i].from = ltt[i].to = 0;
ltt[i].dis = 0;
}
tot = 0;
}
int newnode(int val, int from, int to) {
tot++;
ltt[tot].val = val;
ltt[tot].from = from;
ltt[tot].to = to;
return tot;
}
void pushdown(int now) {
int ls = ltt[now].lson, rs = ltt[now].rson;
ltt[ls].val += ltt[now].tag;
ltt[rs].val += ltt[now].tag;
ltt[ls].tag += ltt[now].tag;
ltt[rs].tag += ltt[now].tag;
ltt[now].tag = 0;
}
int merge(int x, int y) {
if (!x || !y) return x + y;
pushdown(x), pushdown(y);
if (ltt[x].val > ltt[y].val) swap(x, y);
ltt[x].rson = merge(ltt[x].rson, y);
if (ltt[ltt[x].rson].dis > ltt[ltt[x].lson].dis)
swap(ltt[x].lson, ltt[x].rson);
ltt[x].dis = ltt[ltt[x].rson].dis + 1;
return x;
}
int del(int rt) {
pushdown(rt);
int ls = ltt[rt].lson;
int rs = ltt[rt].rson;
return merge(ls, rs);
}
};
leftist_tree ltt;
int root[N], fa[N];
int sta[N], top;
bool vis[N];
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void add_edge(int u, int v, int w) {
int lp = ltt.newnode(w, u, v);
root[v] = ltt.merge(root[v], lp);
}
int dmst(int n, int r) {
for (int i = 1; i <= n; i++) {
int x = i, y = i % n + 1;
int p = ltt.newnode(inf, x, y);
root[y] = ltt.merge(root[y], p);
}
for (int i = 1; i <= 2 * n; i++) fa[i] = i;
sta[++top] = r, vis[r] = true;
int ans = 0, cnt = n;
while (root[sta[top]]) {
int lp = root[sta[top]];
ltt_node tmp = ltt.ltt[lp];
int u = find(tmp.from);
if (u == sta[top]) {
root[sta[top]] = ltt.del(root[sta[top]]);
continue;
}
if (!vis[u]) {
sta[++top] = u, vis[u] = true;
continue;
}
int p = ++cnt;
while (vis[u]) {
int v = sta[top--];
vis[v] = false, fa[v] = p;
int val = ltt.ltt[root[v]].val;
ltt.ltt[root[v]].tag -= val;
int x = find(ltt.ltt[root[v]].to);
ans += (x != find(r)) * val;
root[v] = ltt.del(root[v]);
root[p] = ltt.merge(root[p], root[v]);
}
sta[++top] = p, vis[p] = true;
}
return ans;
}
}
int n, m, rt;
void work() {
cin >> n >> m, rt = 1;
fill(Edmonds::root + 1, Edmonds::root + 2 * n + 1, 0);
fill(Edmonds::fa + 1, Edmonds::fa + 2 * n + 1, 0);
fill(Edmonds::sta + 1, Edmonds::sta + 2 * n + 1, 0);
fill(Edmonds::vis + 1, Edmonds::vis + 2 * n + 1, false);
Edmonds::top = 0, Edmonds::ltt.init();
rep (i, 1, m) {
int u, v, w; cin >> u >> v >> w;
Edmonds::add_edge(u, v, w);
}
ll ans = Edmonds::dmst(n, rt);
cout << (ans >= inf ? -1 : ans) << '\n';
}
int main() {
cin.tie(nullptr) -> ios::sync_with_stdio(false);
int t; cin >> t;
while (t--) work();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 9744kb
input:
3 5 6 1 2 4 1 3 2 2 3 0 3 4 1 4 5 1 5 2 1 4 4 1 2 4 1 3 6 1 4 8 4 2 1000000 3 3 1 2 100 2 1 10 2 3 1000
output:
5 18 1100
result:
ok 3 number(s): "5 18 1100"
Test #2:
score: -100
Wrong Answer
time: 47ms
memory: 9760kb
input:
50000 4 5 2 4 998973548 2 3 501271695 4 1 778395982 1 4 32454891 1 2 757288934 4 5 1 4 720856669 2 3 665098951 3 4 407461517 2 1 866914401 1 2 457859826 4 5 1 2 75288664 1 4 624893594 3 2 458973866 2 4 769074655 2 3 834773751 4 5 2 3 237060634 2 4 297307882 3 4 200383586 1 2 42856502 3 1 16574713 4 ...
output:
-1 -1 -1 480300722 -1 -1 -1801117808 858095911 -1 793861088 605832428 -1 612891589 -1 517769091 -1 -1 93634961 960978736 984886788 -1 -1 -1 -1 -1 977157479 -1 654475526 -1 -1 -2111996458 891249735 -1 885338889 -1 -1 951718998 -1 -1 -1 -1 -1 861533717 -1 -1 598029931 -1 -1 918803214 -1 -1 -1 95324277...
result:
wrong answer 1st numbers differ - expected: '1291015520', found: '-1'