求助 ABC F
  • 板块题目总版
  • 楼主xiaoming007
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/11/11 21:47
  • 上次更新2023/11/12 07:04:12
查看原帖
求助 ABC F
938449
xiaoming007楼主2023/11/11 21:47

求调,用冰茶几维护

#pragma GCC optimize(3, "Ofast", "inline")
#include <ctime>
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
#define debug puts("qwq")
#define open(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define close fclose(stdin);fclose(stdout);
#define aint(x) x.begin(), x.end()
namespace FastIO {
	template <typename T = int>
	inline T read() {
		T s = 0, w = 1;
		char c = getchar();
		while (!isdigit(c)) {
			if (c == '-') w = -1;
			c = getchar();
		}
		while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
		return s * w;
	}
	template <typename T>
	inline void read(T &s) {
		s = 0;
		int w = 1;
		char c = getchar();
		while (!isdigit(c)) {
			if (c == '-') w = -1;
			c = getchar();
		}
		while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
		s = s * w;
	}
	template <typename T, typename... Args> inline void read(T &x, Args &...args) {
		read(x), read(args...);
	}
	template <typename T>
	inline void write(T x, char ch) {
		if (x < 0) x = -x, putchar('-');
		static char stk[25];
		int top = 0;
		do {
			stk[top++] = x % 10 + '0', x /= 10;
		} while (x);
		while (top) putchar(stk[--top]);
		putchar(ch);
		return;
	}
} using namespace FastIO;
int f[201010], val[201010], siz[201010];
int fd(int x) {
	if (x == f[x]) return x;
	return f[x] = fd(f[x]);
}
typedef pair<int, int> pii;
vector<pii> v[201001];
void dfs(int u, int fa) {
	for (auto i : v[u]) {
		if (i.first == fa) continue;
		val[i.first] = val[u] + i.second;
		dfs(i.first, u);
	}
}
void merge(int a, int b, int d) {
	int fa = fd(a), fb = fd(b);
	if (siz[fa] > siz[fb]) {
		swap(a, b);
		swap(fa, fb);
		d = -d;
	}
	f[fa] = fb;
	siz[fa] += siz[fb];
	val[a] = val[b] + d;
	dfs(a, -1);
	v[a].emplace_back(b, -d);
	v[b].emplace_back(a, d);
}
signed main() {
	int n, q;
	read(n, q);
	vector<int> ans;
	for (int i = 1; i <= n; ++i) {
		f[i] = i;
		siz[i] = 1;
	}
	for (int i = 1; i <= q; ++i) {
		int a, b, d;
		read(a, b, d);
		//cout << fd(a) << ' ' << fd(b) << '\n';
		if (fd(a) == fd(b)) {
			if (val[a] - val[b] == d) ans.emplace_back(i);
		} else {
			ans.emplace_back(i);
			merge(a, b, d);
		}
	}
	for (auto i : ans) write(i, ' ');
	return 0;
}
2023/11/11 21:47
加载中...