bzoj 5206
Posted shandongs1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj 5206相关的知识,希望对你有一定的参考价值。
$n$ 点 $m$ 边图的有限制三元环个数
首先将所有左右端点并且属性相同的边的权值相加,合并为一条边
在这只之前得先排序
排序之前得先判断是否需要交换左右端点的位置 T_T
然后统计三元环
补充说明按照上一篇博客的做法统计的正确性
考虑一个三元环 $(u, v), (v, v_2), (u, v_2)$
建边之后一定存在且只存在一个点的出度为2,这也就是改图成为 $DAG$ 的原因
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; #define gc getchar() inline int read() {int x = 0; char c = gc; while(c < ‘0‘ || c > ‘9‘) c = gc; while(c >= ‘0‘ && c <= ‘9‘) x = x * 10 + c - ‘0‘, c = gc; return x;} #undef gc const int N = 5e4 + 10, M = 1e5 + 10, Mod = 1e9 + 7; int n, m; int A[M], B[M], W[M], C[M], Id[M]; int du[N]; int vis[N][4], cost[N][4]; inline int Get_() { char c = getchar(); return c == ‘R‘ ? 1 : (c == ‘G‘ ? 2 : 3); } inline bool Cmp(const int &a, const int &b) { if(A[a] != A[b]) return A[a] < A[b]; if(B[a] != B[b]) return B[a] < B[b]; return C[a] < C[b]; } int cnt, head[N]; struct Node {int v, w, nxt, col;} G[M]; inline void Add(int u, int v, int w, int col) { G[++ cnt].v = v; G[cnt].w = w; G[cnt].col = col; G[cnt].nxt = head[u]; head[u] = cnt; } int main() { n = read(), m = read(); for(int i = 1; i <= m; i ++) { A[i] = read(), B[i] = read(), W[i] = read(), C[i] = Get_(), Id[i] = i; if(A[i] > B[i]) swap(A[i], B[i]); } sort(Id + 1, Id + m + 1, Cmp); int t = 1; for(int i = 2; i <= m; i ++) { if(A[Id[i]] == A[Id[t]] && B[Id[i]] == B[Id[t]] && C[Id[i]] == C[Id[t]]) W[Id[t]] = (W[Id[t]] + W[Id[i]]) % Mod; else t ++, Id[t] = Id[i]; } m = t; for(int i = 1; i <= m; i ++) du[A[Id[i]]] ++, du[B[Id[i]]] ++; for(int i = 1; i <= n; i ++) head[i] = -1; for(int i = 1; i <= m; i ++) { if(du[A[Id[i]]] > du[B[Id[i]]] || (du[A[Id[i]]] == du[B[Id[i]]] && A[Id[i]] > B[Id[i]])) Add(B[Id[i]], A[Id[i]], W[Id[i]], C[Id[i]]); else Add(A[Id[i]], B[Id[i]], W[Id[i]], C[Id[i]]); } long long Answer(0); for(int k = 1; k <= m; k ++) { for(int i = head[A[Id[k]]]; ~ i; i = G[i].nxt) if(G[i].col != C[Id[k]]) vis[G[i].v][G[i].col] = k, cost[G[i].v][G[i].col] = G[i].w; long long tot(0); for(int i = head[B[Id[k]]]; ~ i; i = G[i].nxt) { if(G[i].col != C[Id[k]]) { int need_col = 6 - C[Id[k]] - G[i].col; if(vis[G[i].v][need_col] == k) tot = (tot + 1LL * cost[G[i].v][need_col] * G[i].w) % Mod; } } Answer = (Answer + (tot * W[Id[k]]) % Mod) % Mod; } cout << Answer << " "; return 0; }
以上是关于bzoj 5206的主要内容,如果未能解决你的问题,请参考以下文章
STAT GU4206/GR5206 Sample Midterm