QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#281199#5548. Increase the Toll FeesNYCU_templateWA 140ms13288kbC++143.9kb2023-12-09 23:02:442023-12-09 23:02:45

Judging History

你现在查看的是最新测评结果

  • [2023-12-09 23:02:45]
  • 评测
  • 测评结果:WA
  • 用时:140ms
  • 内存:13288kb
  • [2023-12-09 23:02:44]
  • 提交

answer

#pragma GCC optimize("Ofast", "unroll-loops", "no-stack-protector")
#include <bits/stdc++.h>
#define pb push_back
#define MP make_pair
#define F first
#define S second
#define mem(x, y) memset((x), (y), sizeof(x))
#define loli ios::sync_with_stdio(0), cin.tie(0);
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename Ta, typename Tb>istream& operator>>(istream &in, pair<Ta, Tb> &p){ return in >> p.first >> p.second; }
template<typename Ta, typename Tb>ostream& operator<<(ostream &out, pair<Ta, Tb> &p){ return out << "(" << p.first << ", " << p.second << ")"; }
template<typename T>void arr_print(T a, T b){ T i = a; for(cout << *i++; i != b; i++) cout << " " << *i; cout << "\n"; }
ostream& print(){ return cout << "\n"; }
template<typename T>ostream& print(T a){ return cout << a << "\n"; }
template<typename T, typename... Args>ostream& print(T a, Args ...args){ cout << a << " "; return print(args...); }

//--------------------Main Code--------------------

struct edge{
	int u, v, w;
	bool operator<(const edge &a)const{
		return w < a.w;
	}
} a[200000];

vector<edge> edges;

vector<pii> v[100001];

bitset<100001> vis;
bitset<200000> used;

int n, boss[100001], sz[100001], mxson[100001], fa[100001], lt[100001], dep[100001], tr[400000], idx[100001], inc = 1;

int find(int x){
	if(boss[x] == x)
		return x;
	return boss[x] = find(boss[x]);
}

void combine(int a, int b){
	boss[find(a)] = find(b);
}

void modify(int rt, int lb, int rb, int x, int k){
	int m = lb + rb >> 1;
	if(lb == x && rb == x)
		return tr[rt] = k, void();
	if(x <= m)
		modify(rt << 1, lb, m, x, k);
	else
		modify(rt << 1 | 1, m + 1, rb, x, k);
	tr[rt] = max(tr[rt << 1], tr[rt << 1 | 1]);
}

int query(int rt, int lb, int rb, int l, int r){
	int m = lb + rb >> 1;
	if(lb == l && r == rb)
		return tr[rt];
	if(r <= m)
		return query(rt << 1, lb, m, l, r);
	if(l > m)
		return query(rt << 1 | 1, m + 1, rb, l, r);
	return max(query(rt << 1, lb, m, l, m), query(rt << 1 | 1, m + 1, rb, m + 1, r));
}

void dfs(int u, int par, int depth){
	vis[u] = sz[u] = 1;
	fa[u] = par;
	dep[u] = depth;
	mxson[u] = -1;
	for(pii i : v[u])
		if(!vis[i.F]){
			dfs(i.F, u, depth + 1);
			sz[u] += sz[i.F];
			if(!~mxson[u] || sz[i.F] > sz[mxson[u]])
				mxson[u] = i.F;
		}
}

void dfs2(int u, int top){
	vis[u] = 1;
	lt[u] = top;
	idx[u] = inc++;
	if(!~mxson[u])
		return;
	dfs2(mxson[u], top);
	for(pii i : v[u]){
		if(!vis[i.F]){
			dfs2(i.F, i.F);
			modify(1, 1, n, idx[i.F], i.S);
		}
		if(i.F == mxson[u])
			modify(1, 1, n, idx[i.F], i.S);
	}
}

int lcamx(int a, int b){
	int mx = 0;
	while(lt[a] != lt[b]){
		if(dep[a] > dep[b])
			swap(a, b);
		if(b == lt[b])
			mx = max(mx, query(1, 1, n, idx[b], idx[b]));
		else
			mx = max(mx, query(1, 1, n, idx[mxson[lt[b]]], idx[b]));
		b = fa[lt[b]];
	}
	if(a == b)
		return mx;
	if(dep[a] < dep[b])
		return max(mx, query(1, 1, n, idx[mxson[a]], idx[b]));
	return max(mx, query(1, 1, n, idx[mxson[b]], idx[a]));
}

int main() {
	int m, c;
	ll ans = 0;
	cin >> n >> m;
	for(int i = 0; i < m; i++)
		cin >> a[i].u >> a[i].v >> a[i].w;
	sort(a, a + m);
	c = n;
	for(int i = 1; i <= n; i++)
		boss[i] = i;
	for(int i = 0; c > 1 && i < m; i++){
		if(find(a[i].u) == find(a[i].v))
			continue;
		combine(a[i].u, a[i].v);
		edges.pb(a[i]);
		used[i] = 1;
		c--;
	}
	c = n;
	for(int i = 1; i <= n; i++)
		boss[i] = i;
	for(int i = 0; c > 1 && i < m; i++){
		if(find(a[i].u) == find(a[i].v) || used[i])
			continue;
		combine(a[i].u, a[i].v);
		v[a[i].u].pb(MP(a[i].v, a[i].w));
		v[a[i].v].pb(MP(a[i].u, a[i].w));
		c--;
	}
	if(c > 1)
		return cout << "-1\n", 0;
	dfs(1, 1, 1);
	vis.reset();
	dfs2(1, 1);
	for(edge i : edges)
		ans += lcamx(i.u, i.v) + 1 - i.w;
	cout << ans << "\n";
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 9716kb

input:

4 6
1 2 2
1 3 5
1 4 5
2 3 3
2 4 5
3 4 4

output:

9

result:

ok single line: '9'

Test #2:

score: 0
Accepted
time: 0ms
memory: 9744kb

input:

3 4
1 2 3
2 3 4
1 3 5
1 3 10

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 2ms
memory: 11624kb

input:

5 10
1 2 14
1 3 14
1 4 9
1 5 15
2 3 8
2 3 10
2 4 13
3 4 8
4 5 10
4 5 15

output:

21

result:

ok single line: '21'

Test #4:

score: 0
Accepted
time: 1ms
memory: 7532kb

input:

2 1
1 2 1

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 78ms
memory: 11140kb

input:

29171 100000
7223 21138 270743473
5598 27967 847631606
12666 26050 847631606
75 15747 270743473
8522 12955 847631606
6069 23750 270743473
18708 22605 847631606
16814 22169 847631606
11550 27119 847631606
562 15959 847631606
9400 11110 270743473
15325 23805 270743473
19169 24404 270743473
6649 12062 ...

output:

16827826868780

result:

ok single line: '16827826868780'

Test #6:

score: 0
Accepted
time: 126ms
memory: 12240kb

input:

47977 200000
10970 47321 440845807
1166 29708 767952745
319 37520 546280762
17581 29425 558321466
22079 26884 344816304
7479 44260 791002634
14685 44163 837529020
1537 10359 330017953
8634 27064 969738917
32950 37192 728271930
34751 42782 63025978
32540 34226 86057211
36786 46050 826927874
30444 436...

output:

-1

result:

ok single line: '-1'

Test #7:

score: 0
Accepted
time: 44ms
memory: 13288kb

input:

28825 57648
9446 22014 286256842
14902 20222 14175
3246 20218 80493268
1783 13768 931622563
11107 24862 918832025
25070 27312 98899079
8535 20222 16037
9184 17491 294248461
8799 17834 456827944
1152 11687 960740527
17849 23045 9706
5774 21436 444202963
5417 23045 3092
20222 20370 11232
16585 20222 1...

output:

28822649262260

result:

ok single line: '28822649262260'

Test #8:

score: -100
Wrong Answer
time: 140ms
memory: 12384kb

input:

22253 200000
46 10310 674985053
2403 19582 788128238
5203 7897 985236456
2709 9034 824557880
14337 20524 230824854
19901 22177 99959362
5067 8568 570267383
13707 21474 610729058
7494 7860 109319713
14473 16182 794578653
21 17055 852110939
19320 21640 844993191
2443 21673 170358534
5941 9705 16465049...

output:

4964774600674

result:

wrong answer 1st lines differ - expected: '3270266304988', found: '4964774600674'