-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecode.cpp
112 lines (77 loc) · 1.96 KB
/
ecode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;
map<ll, ll> dp;
#define trav(a, x) for (auto& a : x)
#define all(x) begin(x), end(x)
#define mp make_pair
struct DSU {
vi e;
void init(ll N) { e = vi(N, -1); }
// get representive component, uses path compression
ll get(ll x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool sameSet(ll a, ll b) { return get(a) == get(b); }
ll size(ll x) { return -e[get(x)]; }
bool unite(ll x, ll y) { // union by size
x = get(x), y = get(y);
if (x == y) return 0;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y];
e[y] = x;
return 1;
}
};
ll cnt = 0;
template <class T> T kruskal(ll N, vector<pair<T, pi>> ed) {
sort(all(ed));
T ans = 0;
DSU D;
D.init(N + 1); // edges that unite are in MST
trav(a, ed)
if (D.unite(a.second.first, a.second.second)) {
ans += a.first;
cnt++;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, m;
cin >> n >> m;
vector<pair<ll, pi>> edge;
for (ll i = 0; i < m; i++) {
ll a, b, c;
cin >> a >> b >> c;
edge.push_back(mp(c, mp(a, b)));
}
ll p, k, a, b, c;
cin >> p >> k >> a >> b >> c;
vi qu(k);
for (ll i = 0; i < p; i++) {
cin >> qu[i];
}
for (ll i = p; i < k; i++) {
qu[i] = (qu[i - 1] * a + b) % c;
}
ll ans = 0;
for (ll i = 0; i < k; i++) {
if (dp.find(qu[i]) != dp.end()) {
ans ^= dp[qu[i]];
continue;
}
vector<pair<ll, pi>> edg2;
for(pair<ll, pi> ed : edge) {
edg2.push_back(mp(abs(ed.first - qu[i]), ed.second));
}
ll balh = kruskal(n, edg2);
dp[qu[i]] = balh;
ans ^= balh;
}
cout << ans << endl;
return 0;
}