您的位置:首页 > 其它

[bzoj3498]cakes(三元环)

2018-03-30 21:40 351 查看
题面

题意基本就是找出所有三元环

n<=100000m<=250000n<=100000m<=250000

我竟然才会这个鬼东西 太菜了qwq

说起来我前几天才知道中国剩余定理2333333

先把双向边,变成单向边

由度数小的点,连向度数大的点,如果度数相等 从小点连向大点

这样之后,每个点连出去的边小于sqrt(m)sqrt(m)

因为原本度数大于sqrt(m)sqrt(m)的点小于sqrt(m)sqrt(m)个,又因为度数大于sqrt(m)sqrt(m)只会连度数更大的,所以度数大于sqrt(m)sqrt(m)的点最多只会连出去sqrt(m)sqrt(m)条有向边。

所以只需从一个点枚举任意两条边,再判断连出去的两个点是否有边即可(hash),复杂度O(m∗sqrt(m))O(m∗sqrt(m))

然后我hash TLE了…

换了种写法,枚举边,再枚举这条边两边的点,遍历这两个点连出去的所有点,打个标记,就能去掉hash了

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
class Dread{
private:
bool isdigit(char ch) { return ch >= '0' && ch <= '9'; }
void Getchar(int &tmp){
char ch; tmp = 0; bool b = true;
while (ch = getchar()){
if (ch == '-') b = false;
if (isdigit(ch)) break;
}
for (; isdigit(ch); ch = getchar()) tmp = tmp * 10 + ch - '0';
if (!b) tmp = -tmp;
}
public:
int Int(){ int x; Getchar(x); return x; }
}Read;
const int maxn = 1e5 + 10;
const int maxm = 2.5e5 + 10;
vector<int> g[maxn];
struct {
int x, y;
}edge[maxm];
int n, m;
int a[maxn], id[maxn];
int d[maxn];
void init(){
n = Read.Int(), m = Read.Int();
for (int i = 1; i <= n; i ++)
a[i] = Read.Int();
for (int i = 1; i <= m; i ++){
edge[i].x = Read.Int(), edge[i].y = Read.Int();
d[edge[i].x] ++, d[edge[i].y] ++;
}
}
bool cmp(int x, int y){
if (d[x] != d[y]) return d[x] < d[y];
return x < y;
}
int vis[maxn];
void work(){
for (int i = 1; i <= m; i ++){
if (!cmp(edge[i].x, edge[i].y))
swap(edge[i].x, edge[i].y);
g[edge[i].x].push_back(edge[i].y);
}
ll ans = 0;
for (int i = 1; i <= m; i ++){
int tmp = max(a[edge[i].x], a[edge[i].y]);
for (int asd = 0; asd < g[edge[i].x].size(); asd ++)
vis[g[edge[i].x][asd]] = i;
for (int asd = 0; asd < g[edge[i].y].size(); asd ++)
if (vis[g[edge[i].y][asd]] == i)
ans += max(a[g[edge[i].y][asd]], tmp);
}
cout <<ans <<endl;
}
int main(){
init();
work();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图论