码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj 3498

时间:2018-08-25 18:44:31      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:getch   swap   long   stream   pre   node   name   main   iostream   

统计三元环

很多代码在bzoj都T诶

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>

#define gc getchar()
inline int read() {int x = 0, f = 1; char c = gc; while(c < 0 || c > 9) {if(c == -) f = -1; c = gc;}
    while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = gc; return x;}
#undef gc

using namespace std;
const int N = 1e5 + 10, M = 2e5 + 5e4 + 10;

int n, m;
int A[M], B[M], W[N];
int du[N];
int vis[N];
int cnt, head[N];
struct Node {int v, nxt;} G[M];

inline void Add(int u, int v) {G[++ cnt].v = v, G[cnt].nxt = head[u], head[u] = cnt;}
inline int Max(int a, int b) {if(a > b) return a; return b;}

int main() {
    n = read(), m = read();
    for(int i = 1; i <= n; i ++) W[i] = read();
    for(int i = 1; i <= m; i ++) {
        A[i] = read(), B[i] = read();
        du[A[i]] ++, du[B[i]] ++;
    }
    for(int i = 1; i <= n; i ++) head[i] = -1;
    for(int i = 1; i <= m; i ++) {
        if(du[A[i]] > du[B[i]] || (du[A[i]] == du[B[i]] && A[i] > B[i])) swap(A[i], B[i]);
        Add(A[i], B[i]);
    }
    long long Answer = 0;
    for(int k = 1; k <= m; k ++) {
        for(int i = head[A[k]]; ~ i; i = G[i].nxt) {
            vis[G[i].v] = k;
        }
        for(int i = head[B[k]]; ~ i; i = G[i].nxt) {
            if(G[i].v != A[k] && vis[G[i].v] == k) {
                Answer += Max(Max(W[A[k]], W[B[k]]), W[G[i].v]);
            }
        }
    }
    printf("%lld", Answer);
    return 0;
}

 

bzoj 3498

标签:getch   swap   long   stream   pre   node   name   main   iostream   

原文地址:https://www.cnblogs.com/shandongs1/p/9534632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!