HDU 3047 Zjnu Stadium (with permission and query set)
Train of Thought: note that the number of rows is infinite. Therefore, two people can be separated by another person x far (separated by x columns, not the same row) at the same time. That is to say, the only difference between the two is that, the distance between the two people is in conflict with that mentioned earlier. So how can we determine the contradiction? Because there is an indirect correlation between each person, we need to deal with the distance of a set, so we can use the permission and query set to deal with it well.
For details, see the code:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define Max (a, B) (a)> (B )? (A) :( B) # define Min (a, B) (a) <(B )? (A) :( B) using namespace std; typedef long ll; typedef long double ld; const ld eps = 1e-9, PI = 3.1415926535897932384626433832795; const int mod = 1000000000 + 7; const int INF = 0x3f3f3f; // & 0x7FFFFFFFconst int seed = 131; const ll INF64 = ll (1e18); const int maxn = 50000 + 10; int T, n, m, p [maxn], dist [maxn]; void init (int n) {for (int I = 1; I <= n; I ++) {p [I] = I; dist [I] = 0 ;}} int _ find (int x ){ If (p [x] = x) return p [x]; int fa = p [x]; p [x] = _ find (p [x]); dist [x] + = dist [fa]; // maintain the return p [x];} inline bool BIN (int, int B, int c) {int x = _ find (a); int y = _ find (B); if (x = y) {if (dist [a] + c! = Dist [B]) return false; // determines whether a conflict exists. return true;} p [y] = x; dist [y] = dist [a]-dist [B] + c; // calculate the relative distance. If a and B are separated from c, add or subtract the value based on the vector, you can find the dist [y]. For more information, see return true;} int a, B, c; int main () {while (~ Scanf ("% d", & n, & m) {init (n); int ans = 0; while (m --) {scanf ("% d", & a, & B, & c); if (! BIN (a, B, c) ++ ans;} printf ("% d \ n", ans);} return 0 ;}