bzoj 1202: [HNOI2005]狡猾的商人

我居然用暴力跑过去了。。。

思路:两个区间合成一个新的区间才会产生冲突, 我们用并查集维护前缀和, 0 - n 个节点分别表示sum[ 0 ] - sum[ n ],

d[ i ] 表示 前缀i 和它的父亲的差值, 那么对于两个在同一个并查集里的来说, 就表示这个区间的值已经知道啦, check一下

就好啦, 否则我们将不连通的两团合并。

并查集

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 #define fi first
 4 #define se second
 5 #define mk make_pair
 6 #define pii pair<int,int>
 7 #define piii pair<int, pair<int,int>>
 8 
 9 using namespace std;
10 
11 const int N = 100 + 7;
12 const int M = 1e4 + 7;
13 const int inf = 0x3f3f3f3f;
14 const LL INF = 0x3f3f3f3f3f3f3f3f;
15 const int mod = 1e9 + 7;
16 
17 int  n, m, d[N], fa[N];
18 
19 int getRoot(int x) {
20     if(fa[x] == x) return x;
21     int t = getRoot(fa[x]);
22     d[x] += d[fa[x]];
23     return fa[x] = t;
24 }
25 int main() {
26     int T; scanf("%d", &T);
27     while(T--) {
28         scanf("%d%d", &n, &m);
29         for(int i = 0; i <= n; i++)
30             d[i] = 0, fa[i] = i;
31         bool flag = true;
32         for(int i = 1; i <= m; i++) {
33             int l, r, v;
34             scanf("%d%d%d", &l, &r, &v); l--;
35             int x = getRoot(l);
36             int y = getRoot(r);
37             if(x != y) {
38                 fa[x] = y;
39                 d[x] = d[r] - d[l] - v;
40             } else if(v != d[r] - d[l]) {
41                 flag = false;
42             }
43         }
44         if(flag) puts("true");
45         else puts("false");
46     }
47     return 0;
48 }
49 /*
50 */

暴力:: 我感觉我memset -1 是有问题的 不知道咋就过了。。

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 #define fi first
 4 #define se second
 5 #define mk make_pair
 6 #define pii pair<int,int>
 7 #define piii pair<int, pair<int,int>>
 8  
 9 using namespace std;
10  
11 const int N = 100 + 7;
12 const int M = 1e4 + 7;
13 const int inf = 0x3f3f3f3f;
14 const LL INF = 0x3f3f3f3f3f3f3f3f;
15 const int mod = 1e9 + 7;
16  
17 int w, n, m, tot;
18  
19 struct node {
20     pii a; int v;
21 }q[1000000];
22 int mp[N][N];
23 queue<node> que;
24 int main() {
25     scanf("%d", &w);
26     while(w--) {
27         memset(mp, -1, sizeof(mp));
28         tot = 0;
29         while(!que.empty()) que.pop();
30  
31         scanf("%d%d", &n, &m);
32         for(int i = 0; i < m; i++) {
33             int l, r, v;
34             scanf("%d%d%d", &l, &r, &v);
35             que.push(node{mk(l, r), v});
36         }
37         bool flag = true;
38         while(!que.empty()) {
39             node u = que.front(); que.pop();
40  
41             if(mp[u.a.fi][u.a.se] != -1) {
42                 if(mp[u.a.fi][u.a.se] != u.v) {
43                     flag = false;
44                     break;
45                 } else {
46                     continue;
47                 }
48             }
49             for(int i = 0; i < tot; i++) {
50                 if(q[i].a.se + 1 == u.a.fi) {
51                     que.push(node{mk(q[i].a.fi, u.a.se), q[i].v + u.v});
52                 }
53  
54                 if(u.a.se + 1 == q[i].a.fi) {
55                     que.push(node{mk(u.a.fi, q[i].a.se), q[i].v + u.v});
56                 }
57             }
58             mp[u.a.fi][u.a.se] = u.v;
59             q[tot++] = u;
60  
61         }
62         if(flag) puts("true");
63         else puts("false");
64     }
65     return 0;
66 }
67 /*
68 */
原文地址:https://www.cnblogs.com/CJLHY/p/9063373.html