Description
Input
Output
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 typedef long long LL; 7 8 const int MAXN = 100010; 9 const int MAXE = 200010; 10 11 struct Edge { 12 int from, to, del; 13 LL w; 14 void read() { 15 scanf("%d%d%I64d", &from, &to, &w); 16 del = 0; 17 if(from > to) swap(from, to); 18 } 19 bool operator < (const Edge &rhs) const { 20 if(from != rhs.from) return from < rhs.from; 21 return to < rhs.to; 22 } 23 bool operator == (const Edge &rhs) const { 24 return from == rhs.from && to == rhs.to; 25 } 26 } edge[MAXE]; 27 28 LL sum[3]; 29 int n, m, q; 30 int head[MAXN], deg[MAXN], ecnt; 31 int col[MAXN]; 32 LL state[MAXN][2]; 33 int to[MAXE], next[MAXE], dir[MAXE]; 34 LL weight[MAXE]; 35 36 void init() { 37 memset(head, 0, sizeof(head)); 38 memset(sum, 0, sizeof(sum)); 39 memset(deg, 0, sizeof(deg)); 40 memset(state, 0, sizeof(state)); 41 ecnt = 2; 42 } 43 44 void add_edge(int u, int v, LL w, bool flag) { 45 to[ecnt] = v; weight[ecnt] = w; dir[ecnt] = flag; next[ecnt] = head[u]; head[u] = ecnt++; 46 } 47 48 void change() { 49 int x, c; 50 scanf("%d", &x); 51 c = col[x]; col[x] = !col[x]; 52 sum[c] -= state[x][0]; 53 sum[c + 1] -= state[x][1]; 54 sum[col[x]] += state[x][0]; 55 sum[col[x] + 1] += state[x][1]; 56 for(int p = head[x]; p; p = next[p]) { 57 int &v = to[p]; 58 if(!dir[p]) { 59 state[v][c] -= weight[p]; 60 state[v][col[x]] += weight[p]; 61 } 62 sum[c + col[v]] -= weight[p]; 63 sum[col[x] + col[v]] += weight[p]; 64 } 65 } 66 67 char s[10]; 68 69 int main() { 70 int t = 0; 71 while(scanf("%d%d", &n, &m) != EOF) { 72 init(); 73 for(int i = 1; i <= n; ++i) scanf("%d", &col[i]); 74 for(int i = 1; i <= m; ++i) { 75 edge[i].read(); 76 sum[col[edge[i].from] + col[edge[i].to]] += edge[i].w; 77 } 78 printf("Case %d:\n", ++t); 79 sort(edge + 1, edge + m + 1); 80 for(int i = 1; i < m; ++i) { 81 if(edge[i] == edge[i + 1]) { 82 edge[i].del = true; 83 edge[i + 1].w += edge[i].w; 84 } else { 85 ++deg[edge[i].from], ++deg[edge[i].to]; 86 } 87 } 88 for(int i = 1; i <= m; ++i) { 89 if(edge[i].del) continue; 90 if(deg[edge[i].from] < deg[edge[i].to]) { 91 add_edge(edge[i].from, edge[i].to, edge[i].w, 0); 92 state[edge[i].to][col[edge[i].from]] += edge[i].w; 93 } else if(deg[edge[i].from] > deg[edge[i].to]) { 94 add_edge(edge[i].to, edge[i].from, edge[i].w, 0); 95 state[edge[i].from][col[edge[i].to]] += edge[i].w; 96 } else { 97 add_edge(edge[i].from, edge[i].to, edge[i].w, 1); 98 add_edge(edge[i].to, edge[i].from, edge[i].w, 1); 99 } 100 } 101 scanf("%d", &q); 102 while(q--) { 103 scanf("%s", s); 104 if(*s == 'A') { 105 int x, y; 106 scanf("%d%d", &x, &y); 107 printf("%I64d\n", sum[x + y]); 108 } else change(); 109 } 110 } 111 }
代码(3078MS)(稍微改了一下):
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 typedef long long LL; 7 8 const int MAXN = 100010; 9 const int MAXE = 200010; 10 11 struct Edge { 12 int from, to, del; 13 LL w; 14 void read() { 15 scanf("%d%d%I64d", &from, &to, &w); 16 del = 0; 17 if(from > to) swap(from, to); 18 } 19 bool operator < (const Edge &rhs) const { 20 if(from != rhs.from) return from < rhs.from; 21 return to < rhs.to; 22 } 23 bool operator == (const Edge &rhs) const { 24 return from == rhs.from && to == rhs.to; 25 } 26 } edge[MAXE]; 27 28 LL sum[3]; 29 int n, m, q; 30 int head[MAXN], deg[MAXN], ecnt; 31 int col[MAXN]; 32 LL state[MAXN][2]; 33 int to[MAXE], next[MAXE]; 34 LL weight[MAXE]; 35 36 void init() { 37 memset(head, 0, sizeof(head)); 38 memset(sum, 0, sizeof(sum)); 39 memset(deg, 0, sizeof(deg)); 40 memset(state, 0, sizeof(state)); 41 ecnt = 2; 42 } 43 44 void add_edge(int u, int v, LL w) { 45 to[ecnt] = v; weight[ecnt] = w; next[ecnt] = head[u]; head[u] = ecnt++; 46 } 47 48 void change() { 49 int x, c; 50 scanf("%d", &x); 51 c = col[x]; col[x] = !col[x]; 52 sum[c] -= state[x][0]; 53 sum[c + 1] -= state[x][1]; 54 sum[col[x]] += state[x][0]; 55 sum[col[x] + 1] += state[x][1]; 56 for(int p = head[x]; p; p = next[p]) { 57 int &v = to[p]; 58 state[v][c] -= weight[p]; 59 state[v][col[x]] += weight[p]; 60 sum[c + col[v]] -= weight[p]; 61 sum[col[x] + col[v]] += weight[p]; 62 } 63 } 64 65 char s[10]; 66 67 int main() { 68 int t = 0; 69 while(scanf("%d%d", &n, &m) != EOF) { 70 init(); 71 for(int i = 1; i <= n; ++i) scanf("%d", &col[i]); 72 for(int i = 1; i <= m; ++i) { 73 edge[i].read(); 74 sum[col[edge[i].from] + col[edge[i].to]] += edge[i].w; 75 } 76 printf("Case %d:\n", ++t); 77 sort(edge + 1, edge + m + 1); 78 for(int i = 1; i < m; ++i) { 79 if(edge[i] == edge[i + 1]) { 80 edge[i].del = true; 81 edge[i + 1].w += edge[i].w; 82 } else { 83 ++deg[edge[i].from], ++deg[edge[i].to]; 84 } 85 } 86 for(int i = 1; i <= m; ++i) { 87 if(edge[i].del) continue; 88 if(deg[edge[i].from] < deg[edge[i].to]) { 89 add_edge(edge[i].from, edge[i].to, edge[i].w); 90 state[edge[i].to][col[edge[i].from]] += edge[i].w; 91 } else { 92 add_edge(edge[i].to, edge[i].from, edge[i].w); 93 state[edge[i].from][col[edge[i].to]] += edge[i].w; 94 } 95 } 96 scanf("%d", &q); 97 while(q--) { 98 scanf("%s", s); 99 if(*s == 'A') { 100 int x, y; 101 scanf("%d%d", &x, &y); 102 printf("%I64d\n", sum[x + y]); 103 } else change(); 104 } 105 } 106 }