强联通分量+缩点

强联通分量,一个有向图中的某一个子图,满足该子图中任意一点,都可以到达该子图中任意点.
计算的方法叫tarjan算法,也就是一个dfs方法,每次对未被访问过的一个点开始,因为如果该点是在一个强联通分量内(当然,假设不止是他一个人),那么该强联通分量肯定是一个”大于环的结构”.那肯定可以dfs下去,关键的地方来了,每个被他所到达的点,如果已经被找到过(特指在寻找该分量时被找到过,而不是此前已经被划入某分量中)或者还未被访问过(全新的),那么需要访问他,并回溯他的low,该low,记录一个点,能够访问到的所有点中最小的dfn值,那么也就是说,一个分量内,所有点除最初点,都会触碰到最初点,降低自己的low值,那么,这样就可以找到最初点了.由此,一个分量也被确定.
缩点是容易的.
hdu 5934

/* Farewell. */
#include <iostream>
#include <vector>
#include <cstdio>
#include <stack>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <set>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MP make_pair
#define MT make_tuple
#define PB push_back
typedef long long  LL;
typedef unsigned long long ULL;
typedef pair<int,int > pii;
typedef pair<LL,LL> pll;
typedef pair<double,double > pdd;
typedef pair<double,int > pdi;
const int INF = 0x7fffffff;
const LL INFF = 0x7f7f7f7fffffffff;
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
const int MAXM = 5e3+17;
const int MOD = 998244353;
const int MAXN = 1e3+17;
struct bomb
{
    LL x,y,r,c;
    bomb(LL a,LL b,LL p,LL d):x(a),y(b),r(p),c(d){}
    bomb(){}
}all[MAXN];
vector<int > G[MAXN];
int DFN[MAXN],Low[MAXN],in[MAXN],instack[MAXN],num,idx;  
stack<int> S;  
int belong[MAXN];
vector<int > GC[MAXN];
LL ct[MAXN];
void tarjan(int u)  
{  
    int v;
    DFN[u] = Low[u] = ++idx;  
    instack[u] = 1;  
    S.push(u);  
    for(int i=0; i<G[u].size(); i++)  
    {  
        v = G[u][i];  
        if(!DFN[v])  
        {  
            tarjan(v);  
            Low[u] = min(Low[u], Low[v]);  
        }  
        else if(instack[v])  
            Low[u] = min(Low[u], DFN[v]);  
    }  
    if(DFN[u] == Low[u])  
    {  
        num++;  
        do  
        {  
            v = S.top();  
            S.pop();  
            instack[v] = 0;
            belong[v] = num;  
        }while(v != u);  
    }
}  
void scc(int n)  
{  
    for(int i=0; i<n; i++)  
        if(!DFN[i])  
            tarjan(i);  
}  
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#endif
    int t,x=1;
    cin>>t;
    while(t--)
    {
        printf("Case #%d: ",x++ );
        while(!S.empty()) S.pop();
        memset(in,0,sizeof(in));
        memset(DFN,0,sizeof(DFN));
        memset(Low,0,sizeof(Low));
        memset(instack,0,sizeof(instack));
        memset(belong,-1,sizeof(belong));
        num = -1;
        idx = 0;
        int n,nc = -1;        
        cin>>n;
        for (int i = 0; i < n; ++i)
        {
            G[i].clear();
            GC[i].clear();
            ct[i] = INFF;
        }
        for (int i = 0; i < n; ++i)
        {
            LL a,b,c,d;
            scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
            all[i] = bomb(a,b,c,d);
        }
        for (int i = 0; i < n; ++i)
        {
            LL x = all[i].x,y = all[i].y;
            for (int j = 0; j < n; ++j)
            {
                if(j==i) continue;
                LL tx = all[j].x,ty = all[j].y;
                LL dis = abs(tx-x)*abs(tx-x)+abs(ty-y)*abs(ty-y);
                if(dis<=all[i].r*all[i].r)
                    G[i].push_back(j);
            }
        }
        scc(n);
        for(int i = 0;i < n;++i)
        {
            debug(i);
            debug(belong[i]);
        }
        for (int i = 0; i < n; ++i)
        {
            int ic = belong[i];
            nc = max(ic,nc);
            ct[ic] = min(ct[ic],all[i].c);
            for (int j = 0; j < G[i].size(); ++j)
                if(ic != belong[G[i][j]])
                {
                    GC[ic].push_back(belong[G[i][j]]);
                    ++in[belong[G[i][j]]];
                }
        }
        LL ans = 0;
        for (int i = 0; i <= nc; ++i)
            if(in[i]==0)
                ans += ct[i];
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值