1454E.Number of Simple Paths

题目大意:

给定一个n个点n条边的无向连通图,求图中简单路径的个数,1->2>3和3->2->1被看做同一个答案。

概念引入:假如一个图就是一个首尾依次相连的环路,那么图上任意两点都有两条简单路径。一颗树上的点之间只有一条路径。

这道题目可以看出是一棵树上加了一条边,那么这棵树上一定存在环。那么一部分点之间就可以通过在环上行走从而使互相之间有两条简单路径,但是一部分点没有,那就是挂在环上的树上的点,他们之间互相只有一条路径。这个图其实是一个基环树。

假如所有点之间都可以通过在环上行走产生2条简单路径,那么答案为n*(n-1),但是这个答案中有一部分是多余的,就是那些挂上环上的树里的点,假设每个树的大小为m1,m2,m3。。。

还要减去所有的mi*(mi-1)/2。找环可以用tarjan,找树的话BFS一下就可以。

// #pragma GCC optimize(2)
// #include <random>
// #include <windows.h>
// #include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cout.tie(0);
#define lson(x) (x << 1)
#define rson(x) (x << 1 | 1)
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e5 + 10;
const int maxm = 1e7 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);
int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
// int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct Edge
{
    int next, to;
} e[maxn << 2];
int head[maxn], dfn[maxn], pre[maxn];
int k = 0, tim = 0;
int n;
bool on[maxn]; // 在环上的点
bool vis[maxn];
bool f = false;
void add(int u, int v)
{
    e[k].next = head[u];
    e[k].to = v;
    head[u] = k++;
}
void Tarjan(int u, int fa)
{
    pre[u] = fa;
    dfn[u] = ++tim;
    for (int i = head[u]; ~i; i = e[i].next)
    {
        int v = e[i].to;
        if (v == fa)
            continue;
        if (!dfn[v])
        {
            Tarjan(v, u);
            if (f == true)
                return;
        }
        else if (dfn[v] < dfn[u] && dfn[v] > 0)
        {
            int t = u;
            on[u] = true;
            while (1)
            {
                on[pre[t]] = true;
                t = pre[t];
                if (v == t)
                    break;
            }
            f = true;
            return;
        }
    }
    return;
}
LL BFS(int s)
{
    queue<int> q;
    q.push(s);
    LL cnt = 0;
    vis[s] = true;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        cnt++;
        for (int i = head[u]; ~i; i = e[i].next)
        {
            int v = e[i].to;
            if (vis[v] == false && on[v] == false)
            {
                vis[v] = true;
                q.push(v);
            }
        }
    }
    return cnt;
}
int main()
{
#ifdef WXY
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    IO;
    int T, x, y;
    cin >> T;
    while (T--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            head[i] = -1, pre[i] = 0, on[i] = false, dfn[i] = 0, vis[i] = false;
        for (int i = 0; i < n; i++)
        {
            cin >> x >> y;
            add(x, y);
            add(y, x);
        }
        f = false;
        Tarjan(1, 0);
        LL ans = 1LL * n * (n - 1);
        for (int i = 1; i <= n; i++)
        {
            if (on[i] == true)
            {
                LL temp = BFS(i);
                ans -= temp * (temp - 1) / 2;
            }
        }
        cout << ans << "\n";
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值