码迷,mamicode.com
首页 > 其他好文 > 详细

P3469 [POI2008]BLO-Blockade 割点 tarjan

时间:2019-02-14 23:42:04      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:pre   fir   eof   hide   scanf   思路   color   event   long   

题意

给定一个无向图,问删掉点i,图中相连的有序对数。(pair<x, y> , x != y);
求每个点对应的答案

思路

首先我们可以发现,如果这个点不是割点,那么答案就是n-1,如果是割点,就要考虑子树中的联通块。
可以用tarjan,O(n)的复杂度

 

技术图片
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

/*
        
⊂_ヽ
  \\ Λ_Λ  来了老弟
   \(‘?‘)
    > ⌒ヽ
   /   へ\
   /  / \\
   ? ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
‘ノ )  L?

*/

using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl ‘\n‘

#define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);


const ll oo = 1ll<<17;
const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e9+7;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<0||ch>9) f|=(ch==-),ch=getchar();
    while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar();
    return x=f?-x:x;
}

inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;}

/*-----------------------showtime----------------------*/
 
            const int maxn = 1e5+9;
            struct E{
                int v,nxt;
            }edge[1000009];
            int head[maxn],gtot;
            ll ans[maxn];
            void addedge(int u,int v){
                edge[gtot].v = v;
                edge[gtot].nxt = head[u];
                head[u] = gtot++;
            }
            int dp[maxn],dfn[maxn],low[maxn];
            int tot = 0;
            int n,m;
            void dfs(int u,int fa){
                dfn[u] = low[u] = ++tot;
                dp[u] = 1;
                int sum = 0;
                for(int i=head[u]; ~i; i = edge[i].nxt){
                    int v = edge[i].v;
                    if(dfn[v] == 0){
                        dfs(v, u);
                        
                        low[u] = min(low[u], low[v]);
                        dp[u] += dp[v];
                        if(low[v] >= dfn[u])
                        {
                            ans[u] += 1ll*sum * dp[v];
                            
                            sum = sum + dp[v];
                        }
                    }
                    else if(v != fa){
                        low[u] = min(low[u], dfn[v]);
                    }
                }
                if(fa != -1)ans[u] = ans[u] + 1ll*(n-sum-1)*sum; 
            }
int main(){
            memset(head, -1, sizeof(head));
            scanf("%d%d", &n, &m);
            for(int i=1; i<=m; i++){
                int u,v;
                scanf("%d%d", &u, &v);
                addedge(u,v);
                addedge(v,u);
            }
            
            dfs(1, -1);
            for(int i=1; i<=n; i++) {
                printf("%lld\n", (ans[i] + 1ll*(n-1)) * 2ll);
            }
            return 0;
}
View Code

 

P3469 [POI2008]BLO-Blockade 割点 tarjan

标签:pre   fir   eof   hide   scanf   思路   color   event   long   

原文地址:https://www.cnblogs.com/ckxkexing/p/10381281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!