[NOIP模拟] star

[NOIP模拟] star


About

    2017.10.30 T3

Solution:

    暴力扫每一个点,再扫该点的连边,每次只统计度数较小的点,其复杂度为 O(mm) , 题解将点分为轻点和重点,其实不用,但是可以用轻点和重点判断复杂度。

Code:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <ctime>
#include <map>
#include <vector>
#define mp make_pair
#define clr(a) memset(a, 0, sizeof(a))
using namespace std;

inline int read() {
    int i = 0, f = 1;
    char ch = getchar();
    while(!isdigit(ch)) {
        if(ch == '-') f = -1; ch = getchar();
    }
    while(isdigit(ch)) {
        i = (i << 3) + (i << 1) + ch - '0'; ch = getchar();
    }
    return i * f;
}

const int MAXN = 1e5 + 5;
const int MAXM = 2e5 + 5;
int first[MAXN], nxt[MAXM * 2], to[MAXM * 2], tot, du[MAXN], vis[MAXN];
int tag[MAXN];

inline void addedge(int x, int y) {
    nxt[++tot] = first[x]; first[x] = tot; to[tot] = y;
    nxt[++tot] = first[y]; first[y] = tot; to[tot] = x; 
}

int main() {
    int t = read();
    while(t--) {
        tot = 1;
        clr(first), clr(nxt), clr(du); clr(tag);
        int n = read(), m = read();
        for(int i = 1; i <= m; ++i) {
            int x = read(), y = read();
            addedge(x, y); ++du[x], ++du[y];
        }
        long long ans = 0;
        for(int i = 1; i <= n; ++i) {
            //vis[i] = 1;
            int u = i;
            for(int l = first[i]; l; l = nxt[l]) tag[to[l]] = u;
            for(int l = first[i]; l; l = nxt[l]) {
                //if(vis[to[l]]) continue;
                int v = to[l], cnt = 0; 
                if(du[u] < du[v] || (du[u] == du[v] && u > v)) continue;
                for(int e = first[v]; e; e = nxt[e]) {
                    int now = to[e];
                    if(tag[now] == u) ++cnt;
                }
                ans += cnt * (cnt - 1) / 2;
            }
        }
        cout<<ans<<'\n';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值