Codeforces 732F tarjan

本文探讨了如何通过合理规划无向图转换为有向图的方式,使每个节点可达节点数的最小值最大化,以此来提高虚拟国家Berland的旅游吸引力。文章提出了一种基于强连通分量的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ≤ nuj ≠ 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


题意:给你一个无向图,现在让你转化为有向图,也就是给每条边赋方向,使得每个点能到达的点数的最小值最大,求出这个值,然后输出路的方向。


题解:对于一个强联通分量,里面的点是可以相互到达的,对于两个相邻的强连通分量,设大小分别为x y,x>y,则如果x指向y,那么ans为y,否则答案为x,所以可得应该把小的分量指向大的分量,所以ans即为max(bcc i),也就是强连通分量最大的那个值。此时其它的联通分量方向都指向这个最大的。

边的方向:对于一个强连通分量,随便找个点,然后dfs出去,每次都是to指向from,即可构成环,所以我们可以从最大的那个强联通分量dfs出去,这样就可以使得所有联通分量指向这个最大的。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int head[400005],cnt=0,n,m,bccnum,ef[800005],st[800005],index,top,dfn[800005],low[800005],bcc[800005],num[800005],now,ans,vis[800005];
struct node{
	int from,to,nex;
}edge[800005];
void add(int u,int v){
	edge[cnt].to=v;
	edge[cnt].from=u;
	edge[cnt].nex=head[u];
	head[u]=cnt++;
}
void tarjan(int root){//求强连通分量
    dfn[root]=low[root]=++index;
    st[++top]=root; 
    for(int i=head[root];~i;i=edge[i].nex){
        int v=edge[i].to;
        if(ef[i])continue;
        ef[i]=ef[i^1]=1;
        if(!dfn[v]){
            tarjan(v);
            low[root]=min(low[root],low[v]);
        }
        else low[root]=min(low[root],dfn[v]);
    }
    if(low[root]==dfn[root]){
        bccnum++;
        for(;;){
            int x=st[top--];
            bcc[x]=bccnum;
            num[bccnum]++;
            if(x==root)break;
        }
        if(num[bccnum]>ans){
        	now=root;//定义now为起始dfs点
			ans=max(ans,num[bccnum]);
        }
    }
}
void dfs(int root){
	if(vis[root])return ;//不加剪枝会t
	vis[root]=1;
	for(int i=head[root];~i;i=edge[i].nex){
		int v=edge[i].to;
		if(ef[i])continue;
		ef[i]=1;//代表走过
		ef[i^1]=2;//代表to指向from
		dfs(v);
	}
}
int main(){
	scanf("%d%d",&n,&m);
	int x,y,i,j;
	memset(head,-1,sizeof(head));
	for(i=1;i<=m;i++){
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	tarjan(1);
	memset(ef,0,sizeof(ef));
	dfs(now);
	printf("%d\n",ans);	
	for(i=0;i<cnt;i++){
		if(ef[i]==2)printf("%d %d\n",edge[i].from,edge[i].to);
	}
	return 0;
}


<think>好的,我现在需要处理用户关于Codeforces题型总结和解题技巧的请求。首先,回顾之前的对话历史,用户之前询问了ACM学习路线,我给出了分阶段的学习计划和资源推荐。现在用户转向Codeforces的具体题型和解题技巧,这表明他们可能正在准备竞赛,需要更针对性的策略。 首先,我需要确定用户的需求。用户想要的是Codeforces平台上各类题型的分类总结以及对应的解题思路。他们可能希望了解常见的题型类别,每个类别的特点,以及解决这些题目的常用技巧和算法。此外,用户可能还希望得到一些具体的例题和代码示例,以便更好地理解和应用这些技巧。 接下来,我需要参考用户提供的引用内容。引用[1]中提到了算法基础,包括复杂度分析、递归、分治、贪心、排序、前缀和、差分、二分、三分、倍增等。这些内容在Codeforces题目中确实常见,因此需要将这些算法和技巧与具体的题型对应起来,进行分类归纳。 我需要确保回答结构清晰,分点列出各个题型类别,每个类别下包括题型特点、解题技巧、例题及解析。同时,按照用户之前的要求,使用中文回答,行内数学公式用$...$,独立公式用$$...$$,并正确使用LaTeX语法。此外,需要生成相关问题,并在适当的地方添加引用标识。 在整理题型时,要覆盖Codeforces常见的题目类型,如贪心、动态规划、图论、数论、数据结构、构造题、二分/三分、字符串处理、计算几何等。每个类别需要简要说明其特点,解题时的常用方法,并举出具体的题目例子,最好附上题目编号,方便用户查找练习。 同时,要注意结合用户之前的学习路线,推荐相关的资源,比如《算法竞赛入门经典》中的例题,或者在线平台如Codeforces本身的训练建议。此外,可能需要提到一些实战技巧,比如如何快速识别题型、如何优化代码等。 需要避免的是过于泛泛而谈,要尽量具体,给出实际可操作的技巧。例如,在贪心题中,可以提到邻项交换法或后悔法的具体应用;在动态规划中,强调状态设计和转移方程的优化方法。对于每个技巧,最好能对应一个例题,说明如何应用该技巧解决问题。 另外,考虑到用户可能需要代码示例,可以在适当的位置插入简短的代码片段,如动态规划或二分的典型实现,但需注意代码的准确性和简洁性,避免冗长。 最后,生成的相关问题需要围绕用户可能的后续疑问展开,比如如何提高刷题效率,特定题型的深入分析方法,或者推荐Codeforces上的训练计划等。这些问题应自然延伸当前主题,帮助用户进一步探索。 现在,需要检查是否覆盖了用户的所有需求,是否结构清晰,是否引用了之前的资源(如引用[^1]),并确保所有数学公式的正确格式。同时,确保回答真实可靠,基于常见的竞赛知识和经验,避免错误的信息。</think>### Codeforces题型总结与解题技巧分类指南 #### 一、高频核心题型分类 1. **贪心算法** - **题型特点**:决策序列中局部最优选择可推导全局最优解,常见于区间调度、资源分配问题 - **解题技巧**: - 邻项交换法(证明交换后更优):如$n$个数的最大拼接问题(CF 1385B) - 后悔法(优先队列维护候选集):CF 1526C(药水选择) ```cpp // 后悔法模板 priority_queue<int, vector<int>, greater<int>> pq; for(int x : arr){ sum += x; pq.push(x); while(sum < 0){ sum -= pq.top(); pq.pop(); } } ``` - 时间轴处理:CF 1481C(栅栏涂色时序处理)[^1] 2. **动态规划** - **状态设计模式**: - 线性DP:$dp[i]$表示前$i$个元素的最优解(CF 474D 花朵问题) - 状态压缩DP:用位运算表示集合状态(CF 580D 美食家问题) - 树形DP:结合DFS遍历处理子树关系(CF 1092F 树权值分配) - **优化技巧**: - 斜率优化:维护凸包降低时间复杂度至$O(n)$(CF 319C 木材切割) - 滚动数组:空间复杂度优化至$O(1)$(CF 1114D 颜色填充) 3. **图论算法** - **高频考点**: | 算法类型 | 应用场景 | 例题 | |----------------|-----------------------------------|----------------| | 拓扑排序 | 任务调度/依赖解析 | CF 1385E | | 强连通分量 | 缩点处理环结构 | CF 427C | | 网络流 | 二分图匹配/资源分配 | CF 1139E | | 最短路变形 | 分层图/边权特殊处理 | CF 1473E | 4. **数论问题** - **核心工具**: - 欧拉定理:处理模指数运算(CF 615D 质因子乘积) - 中国剩余定理:同余方程组求解(CF 687B) - 莫比乌斯反演:统计互质对数量(CF 547C) - **特殊技巧**: $$ \sum_{d|n}\mu(d) = \begin{cases} 1 & n=1 \\ 0 & n>1 \end{cases} $$ 用于快速计算GCD相关问题 #### 二、特征题型专项突破 1. **构造题(Constructive Algorithms)** - **识别特征**:输出满足特定条件的结构而非数值结果 - **经典解法**: - 奇偶性构造:CF 1421B(矩阵路径奇偶性) - 平衡分配法:CF 1343D(调整数组使对称元素相等) - 极端值构造:CF 1375E(通过逆序操作排序数组) 2. **交互题(Interactive Problems)** - **解题框架**: ```python low, high = 1, 1e18 while low <= high: mid = (low + high) // 2 print(f"? {mid}") response = input() if response == ">=": low = mid + 1 else: high = mid - 1 print(f"! {high}") ``` - **注意事项**: - 刷新输出缓冲区(C++用`cout << endl`) - 错误处理:CF 1520F2(二进制猜数容错机制) 3. **数据结构题** - **题型矩阵**: | 数据结构 | 适用场景 | 优化技巧 | |----------------|-------------------------------|---------------------------| | 线段树 | 区间查询/更新 | 延迟标记(lazy propagation)| | 并查集 | 连通性检测 | 路径压缩+按秩合并 | | 字典树 | 前缀匹配统计 | 二进制位处理 | | 单调队列 | 滑动窗口最值 | 淘汰非最优候选 | #### 三、实战技巧体系 1. **复杂度预判法**: - 输入规模$n$与可接受时间复杂度对应表: $$ \begin{array}{c|c} n \leq 10^6 & O(n) \\ n \leq 10^5 & O(n \log n) \\ n \leq 10^4 & O(n^2) \\ n \leq 20 & O(2^n) \\ \end{array} $$ 2. **Debug技巧**: - 边界测试:$n=0,1$等极端情况 - 对拍验证:生成随机数据与暴力程序对比 - 输出中间状态:使用`cerr`输出调试信息 3. **代码模板管理**: - 建议维护以下模板库: ```text /templates ├── graph │ ├── dijkstra.cpp │ └── tarjan_scc.cpp ├── math │ ├── matrix_fastpow.cpp │ └── fft_template.cpp └── ds ├── segment_tree.cpp └── union_find.cpp ``` #### 四、经典例题解析 1. **CF 1485D - Multiples and Power Differences** - **题型**:构造题+数论 - **关键思路**: 构造矩阵元素$a_{i,j} = 720720 + (i+j)\%2 \times (a_{i,j}^4)$ 保证相邻元素差为四次方数 2. **CF 1513C - Add One** - **解法**:动态规划预处理 定义$dp[d][m]$表示数字$d$经过$m$次操作的位数 状态转移: $$ dp[d][m] = \begin{cases} 1 & m=0 \\ dp[1][m-1] + dp[0][m-1] & d=9 \\ dp[d+1][m-1] & otherwise \end{cases} $$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值