[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';
}
}