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

P1726 上白泽慧音 tarjan 模板

时间:2019-02-04 14:16:00      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:clu   queue   ons   http   line   phi   --   time   ace   

P1726 上白泽慧音

这是一道用tarjan做的模板,要求找到有向图中最大的联通块。

技术图片
#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>

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 OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que;

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 = 1000000007;
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;
}
/*-----------------------showtime----------------------*/
            const int maxn = 1e5+9;
            struct E{
                int u,v;
                int nxt;
            }edge[maxn];
            int head[maxn],gtot = 0;
            void addedge(int u,int v){
                edge[gtot].u = u;
                edge[gtot].v = v;
                edge[gtot].nxt = head[u];
                head[u] = gtot++;
            }

            priority_queue<int,vector<int>,greater<int> >tmp,ans;//这是一个小根堆q

            int vis[maxn],dfn[maxn],low[maxn];
            stack<int>sk;
            int tot = 0;
            void tarjan(int u){
                tot++;
                dfn[u] = low[u] = tot;
                vis[u] = 1; sk.push(u);

                for(int i=head[u]; ~i; i = edge[i].nxt){
                    int v = edge[i].v;
                    if(dfn[v] == 0) {
                        tarjan(v);
                        low[u] = min(low[u], low[v]);
                    }
                    else if(vis[v]){
                        low[u] = min(low[u], dfn[v]);
                    }
                }

                if(dfn[u] == low[u]){

                    while(!tmp.empty())tmp.pop();

                    while(!sk.empty() && sk.top() != u){
                        vis[sk.top()] = 0;
                        tmp.push(sk.top()); sk.pop();
                    }   
                    vis[sk.top()] = 0;
                    tmp.push(sk.top()); sk.pop();

                    if(tmp.size() > ans.size()) ans = tmp;
                    else if(tmp.size() == ans.size() && tmp.top() < ans.top()) ans = tmp;
                }
            }
 int main(){        
            int n,m;
            scanf("%d%d", &n, &m);
            memset(head, -1, sizeof(head));
            for(int i=1; i<=m; i++){
                int u,v,op;
                scanf("%d%d%d", &u, &v, &op);
                if(op == 1) addedge(u,v);
                else {
                    addedge(u,v);
                    addedge(v,u);
                }
            }
            for(int i=1; i<=n; i++)
              if(dfn[i] == 0)tarjan(i);
            printf("%d\n", (int)ans.size());
            while(!ans.empty()){
                printf("%d ", ans.top());
                ans.pop();
            }
            puts("");
            return 0;
 }
View Code

 

P1726 上白泽慧音 tarjan 模板

标签:clu   queue   ons   http   line   phi   --   time   ace   

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

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