@hdu - [email protected] Counting Stars

目录

  • @[email protected]
  • @[email protected]
  • @accepted [email protected]
  • @[email protected]

@[email protected]

给定一个 n 点 m 边的无向图(无重边自环),求有多少子图形如,包含 4 个点 {A, B, C, D} 与 6 条边 {AB, BC, CD, DA, AC}。

原题链接。

@[email protected]

一个并不常用的黑科技:三元环计数。

mark一下博客地址。

注意到题目所说的子图其实就是两个三元环共一条边。
我们只需要根据三元环计数的过程统计每条边对应多少三元环,最后每条边的贡献即这条边对应的三元环任意取 2 个的方案数。

@accepted [email protected]

#include <cstdio>

typedef long long ll;

const int MAXN = 100000;
const int MAXM = 200000;

struct edge{
    int to, id;
    edge *nxt;
}edges[MAXM + 5], *adj[MAXN + 5], *ecnt;

void addedge(int u, int v, int i) {
    edge *p = (++ecnt);
    p->to = v, p->id = i, p->nxt = adj[u], adj[u] = p;
//  printf("%d %d : %d\n", u, v, i);
}

int n, m;
int deg[MAXN + 5], u[MAXM + 5], v[MAXM + 5];

ll f[MAXM + 5];

void clear() {
    for(int i=1;i<=n;i++) adj[i] = NULL, deg[i] = 0;
    for(int i=1;i<=m;i++) f[i] = 0;
    ecnt = edges;
}

int tag[MAXN + 5];

int main() {
    while( scanf("%d%d", &n, &m) == 2 ) {
        clear();
        for(int i=1;i<=m;i++)
            scanf("%d%d", &u[i], &v[i]), deg[u[i]]++, deg[v[i]]++;
        for(int i=1;i<=m;i++)
            if( deg[u[i]] > deg[v[i]] || (deg[u[i]] == deg[v[i]] && u[i] > v[i]) )
                addedge(u[i], v[i], i);
            else addedge(v[i], u[i], i);
        for(int i=1;i<=n;i++) {
            for(edge *p=adj[i];p;p=p->nxt) tag[p->to] = p->id;
            for(edge *p=adj[i];p;p=p->nxt)
                for(edge *q=adj[p->to];q;q=q->nxt)
                    if( tag[q->to] ) f[p->id]++, f[q->id]++, f[tag[q->to]]++;
            for(edge *p=adj[i];p;p=p->nxt) tag[p->to] = 0;
        }
        ll ans = 0;
        for(int i=1;i<=m;i++)
            ans += f[i]*(f[i] - 1)/2;
        printf("%lld\n", ans);
    }
}

@[email protected]

感觉这个重构成 DAG 的过程以及之后正确时间复杂度的分析奥妙重重。。。

这个思路如果拓展开来的话,应该还可以有一些很有趣的应用吧(比如「SDOI2018」旧试题)。

原文地址:https://www.cnblogs.com/Tiw-Air-OAO/p/12225809.html

时间: 2024-07-29 02:19:14

@hdu - [email protected] Counting Stars的相关文章

@hdu - [email&#160;protected] Problem A.Alkane

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 求包含 n 个碳的烷烃与烷基的同分异构体个数 mod 998244353. 如果你没学过有机化学,你可以认为烷烃是 n 个点且每个点度数 <= 4 的无根树:烷基是 n 个点且每个点儿子个数 <= 3 的有根树. 原题传送门. @[email protected] 先考虑有根树的情况

@hdu - [email&#160;protected] Rigid Frameworks

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 如果对于一个平面图,将边看成火柴棍,将点看成用橡皮筋将木棍的头绑在一起(请自行脑补).如果这个平面图不能够改变形状,称这个平面图为刚体图. 如下图中只有 D 不是刚体图. 给定一个 n*m 的方格图,你可以在某些方格的对角线上加支撑木棍以保持它的形状不变. 问让一个 n*m 的方格图变

@hdu - [email&#160;protected] Function

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 给定 n,求: \[\sum_{i=1}^{n}gcd(\lfloor^3\sqrt{i}\rfloor, i)\mod 998244353\] Input 第一行包含一个整数 T(1≤T≤11) 描述数据组数. 接下来 T 行,每行一个整数 n (1≤n≤10^21) 描述询问. O

@hdu - [email&#160;protected] Paint Pearls

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 给定一个长度为 n 的序列,每一个位置有一个目标颜色,初始所有位置都没有颜色. 每次操作可以选择一个区间,将这个区间内的位置的颜色改为其目标颜色,代价是区间内不同的目标颜色数量^2. 求将所有位置改为目标颜色的最小代价. Input 多组数据. 每组数据第一行一个整数 n(1 ≤ n

@hdu - [email&#160;protected] Subsequence

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 给定如下计算序列权值的函数: 对于一个由三元组 (cost0, cost1, color) 组成的序列 A,求通过以上函数计算出来的第 k 大的子序列的权值. Input 第一行一个整数 t,表示数据组数. 对于每组数据,第一行包含两个整数 n, k. 接下来 n 行,每行三个整数 c

HDU 6184 Counting Stars

Problem Description Little A is an astronomy lover, and he has found that the sky was so beautiful!So he is counting stars now!There are n stars in the sky, and little A has connected them by m non-directional edges.It is guranteed that no edges conn

$*和[email&#160;protected]之间区别代码分析

#!/bin/bash set 'apple pie' pears peaches for i in $*           /*单引号被去掉,循环单个字符输出*/ do echo $i done [[email protected] Ex_14.02-14.31]# sh 14-14-1 apple pie pears peaches -------------------------------------------------------------- #!/bin/bash set

[email&#160;protected]一个高效的配置管理工具--Ansible configure management--翻译(六)

无书面许可请勿转载 高级playbook Finding files with variables All modules can take variables as part of their arguments by dereferencing them with {{ and }} . You can use this to load a particular file based on a variable. For example, you might want to select a

【转载】 ERROR 1045 (28000): Access denied for user [email&#160;protected] (using password: NO)

来自:http://www.jb51.net/LINUXjishu/10981.html 错误描述: Mysql中添加用户之后可能出现登录时提示ERROR 1045 (28000): Access denied for user的错误.删除user.user中值为NULL的,或更新NULL为test 1)delete from user where user is NULL 2)update user set user='test' where user is NULL.意外的情况: 如果上述方