codeforces round 377 div2 F Tourist Reform tarjan求边双连通分量

F. Tourist Reform
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Berland is a tourist country! At least, it can become such — the government of Berland is confident about this.

There are n cities in Berland, some pairs of which are connected by two-ways roads. Each road connects two different cities. In Berland there are no roads which connect the same pair of cities. It is possible to get from any city to any other city using given two-ways roads.

According to the reform each road will become one-way. It will be oriented to one of two directions.

To maximize the tourist attraction of Berland, after the reform for each city i the value ri will be calculated. It will equal to the number of cities x for which there is an oriented path from the city i to the city x. In other words, ri will equal the number of cities which can be reached from the city i by roads.

The government is sure that tourist's attention will be focused on the minimum value of ri.

Help the government of Berland make the reform to maximize the minimum of ri.

Input

The first line contains two integers n, m (2 ≤ n ≤ 400 000, 1 ≤ m ≤ 400 000) — the number of cities and the number of roads.

The next m lines describe roads in Berland: the j-th of them contains two integers uj and vj (1 ≤ uj, vj ≤ n, uj ≠ vj), where uj and vj are the numbers of cities which are connected by the j-th road.

The cities are numbered from 1 to n. It is guaranteed that it is possible to get from any city to any other by following two-ways roads. In Berland there are no roads which connect the same pair of cities.

Output

In the first line print single integer — the maximum possible value min1 ≤ i ≤ n{ri} after the orientation of roads.

The next m lines must contain the description of roads after the orientation: the j-th of them must contain two integers uj, vj, it means that the j-th road will be directed from the city uj to the city vj. Print roads in the same order as they are given in the input data.

Example
Input
7 9
4 3
2 6
7 1
4 1
7 3
3 5
7 4
6 5
2 5
Output
4
4 3
6 2
7 1
1 4
3 7
5 3
7 4
5 6
2 5


题目描述:给出一个有n(0 < n < 4e5)个点和m(0 < m < 4e5)条边的连通的无重边无向图,现在给每条边规定一个方向,图中所有边都有了方向之后,ri表示点i所能直接或间接到达的点的数量,要求给出一种规定方向的方式,使得所有ri中的最小值最大。


题目思路:规定方向后,应该使得尽量多的点相互可达,而每一个边双连通分量,就是在规定方向之后能够变为强连通分量的最大点集。因此,首先要将所有的边双连通分量都求出来,然后在每个边双连通分量内部dfs一次将边双连通分量变为强连通分量。将一个边双连通分量看作一个节点的话,图中剩下的就是一棵树,找到包含点的个数最多的边双连通分量,以他所在的节点作为根,dfs这棵树,让所有的树边都指向父亲节点,这样标记方向就满足题意了。在这种构图下ri的最小值就是点数最多的边双连通分量中所包含的点的个数。


收获:原来这就叫tarjan缩点...

#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#include<bitset>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
const int maxn = 4e5 + 5;
struct Edge
{
    int u , v , is;
    Edge(int u = 0 , int v = 0 , int is = 0) : u(u) , v(v) , is(is){}
}e[maxn * 3];
int  n , m , ecnt , stamp , dfn[maxn] , low[maxn] , bccno[maxn] ,num[maxn] ,  bcc_cnt ;
vector<int>vec[maxn] , bcc[maxn];
vector<Edge>ans;
bool isbridge[maxn * 2] , vis[maxn * 2];
void add_edge(int u , int v)
{
    Edge e1 = Edge(u , v);
    e[ecnt++] = e1;
    vec[u].push_back(ecnt - 1);
    Edge e2 = Edge(v , u);
    e[ecnt++] = e2;
    vec[v].push_back(ecnt - 1);
}
void tarjan(int index , int fa)
{
    int tmp ;
    dfn[index] = low[index] = ++stamp;
    for(int i = 0 ; i < vec[index].size() ; i++){
        tmp = e[vec[index][i]].v;
        if(!dfn[tmp]){
            tarjan(tmp , index);
            low[index] = min(low[index] , low[tmp]);
            if(low[tmp] > dfn[index]){
                isbridge[vec[index][i]] = isbridge[vec[index][i] ^ 1] = 1;
            }
        }
        else if(dfn[tmp] < dfn[index] && tmp != fa){
            low[index] = min(low[index] , dfn[tmp]);
        }
    }
}

void dfs(int index)
{
    dfn[index] = 1;
    bccno[index] = bcc_cnt;
    ++num[bcc_cnt];
    for(int i = 0 ; i < vec[index].size() ; i++){
        int tmp = vec[index][i];
        if(isbridge[tmp])       continue;
        if(!dfn[e[tmp].v])
            dfs(e[tmp].v);
    }
}

void find_ebcc()
{
    bcc_cnt = stamp = 0;
    mem(dfn , 0);
    mem(low , 0);
    mem(isbridge , 0);
    mem(bccno , 0);
    mem(bcc , 0);
    for(int i = 1 ; i <= n ; i++){
        if(!dfn[i])
            tarjan(i , -1);
    }
    mem(dfn , 0);
    for(int i = 1 ; i<= n ; i++){
        if(!dfn[i]){
            bcc_cnt ++;
            dfs(i);
        }
    }
}

void dfs2(int cur)      //点可以重复,边不能重复,每条边被访问两次
{
    for(int i = 0 ; i< vec[cur].size() ; i++){
        int tmp = vec[cur][i];
        if(vis[tmp])        continue;
        Edge &ed = e[vec[cur][i]];
        int nt = ed.v;
        if(bccno[nt] != bccno[cur])     continue;
        ed.is = 1;
        vis[tmp ^ 1] = vis[tmp] = 1;
        dfs2(nt);
    }
}

void dfs3(int cur)
{
    dfn[cur] = 1;
    for(int i = 0 ; i < vec[cur].size() ; i++){
        int tmp = vec[cur][i];
        Edge ed = e[tmp];
        int nt = ed.v;
        if(dfn[nt])     continue;
        if(isbridge[tmp]){
            e[tmp ^ 1].is = 1;
        }
        dfs3(nt);
    }
}
int main()
{
    int u , v , res = -1 , max_id;
    ecnt = 0;
    scanf("%d %d" , &n , &m);
    for(int i = 1 ; i <= m ; i++){
        scanf("%d %d" , &u , &v);
        add_edge(u , v);
    }
    find_ebcc();
    mem(vis , 0);
    for(int i = 1 ; i <= n ; i++){
        dfs2(i);
    }
    for(int i = 1 ; i <= bcc_cnt ; i++){        //找到最大的边双连通分量
        if(num[i] > res){
            max_id  =  i;
            res = num[i];
        }
    }
    for(int i = 1 ; i<= n ; i++){       //找到双连通分量的一个点以进行dfs3
        if(bccno[i] == max_id){
            max_id = i;     break;
        }
    }
    mem(dfn , 0);
    mem(vis , 0);
    dfs3(max_id);
    for(int i = 0 ; i < ecnt ; i++){
        if(vis[i])      continue;
        if(e[i].is == 1 )
            ans.push_back(e[i]);
        else
            ans.push_back(e[i ^ 1]);
        vis[i] = vis[i ^ 1] = 1;
    }
    printf("%d\n",res);
    for(int i =0 ; i < ans.size() ; i++){
        printf("%d %d\n" , ans[i].u , ans[i].v);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出层预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接层来得到预测值。其网络结构通常包含多个卷积层和全连接层,通过卷积层提取图像特征,通过全连接层输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值