CodeForces - 791B 并查集或dfs

本文介绍了一个社交网络合理性的检查算法,通过并查集或DFS实现,确保任意三个成员若两两互为朋友,则三者间也必须全部为朋友关系。

Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures).

There are n members, numbered 1 through nm pairs of members are friends. Of course, a member can't be a friend with themselves.

Let A-B denote that members A and B are friends. Limak thinks that a network isreasonable if and only if the following condition is satisfied: For every threedistinct members (XYZ), if X-Y and Y-Z then also X-Z.

For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well.

Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes.

Input

The first line of the input contain two integers n and m (3 ≤ n ≤ 150 000) — the number of members and the number of pairs of members that are friends.

The i-th of the next m lines contains two distinct integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input.

Output

If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes).

Example
Input
4 3
1 3
3 4
1 4
Output
YES
Input
4 4
3 1
2 3
3 4
1 2
Output
NO
Input
10 4
4 3
5 10
8 9
1 2
Output
YES
Input
3 2
1 2
2 3
Output
NO
Note

The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not.


题意:给定N个点M条路来描述两个点之间的关系,并且A-B,B-C,那么A-C一定要有边,问你给定的符不符合要求;

思路:难道说最近数论已将我石化?大清早就那么zz;水题并查集[dfs也可以,表达方式不同,思路一样];

搭建满足条件的所有树,统计树中元素为v,则这棵树上的边有且仅有v*(v-1)/2,也即是每个点要和其所在树每个点都有边,其余为NO;

#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
const int MAX_N = 150005;

int n,m,a,b;
int fa[MAX_N];
int du[MAX_N];
int ran[MAX_N];

int found(int x) {
    return x == fa[x]?x:fa[x] = found(fa[x]);
}

void bing(int x ,int y) {
    x = found(x);
    y = found(y);
    if(x != y) {
        fa[x] = y;
    }
}

int main() {
    scanf("%d %d",&n,&m);
    for(int i = 0; i <=n; i++) {
        fa[i] = i;
        du[i] = 0;
        ran[i] = 0;
    }
    for(int i = 0; i < m; i++) {
        scanf("%d %d",&a,&b);
        bing(a,b);
        du[a]++;
        du[b]++;
    }
    for(int i = 0;i <= n;i++){
        int x = found(i);
        ran[x]++;
    }
    int flag = 0;
    for(int i = 0; i <= n; i++) {
         int x = found(i);
         if(du[i] != ran[x]-1){
            flag = 1;
            break;
         }
    }
    if(!flag)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;

int vis[150500],degree[150500];
vector<int> mp[150500];
queue<int> s;
int flag;

void Dfs(int u)
{
    vis[u]=1;
    s.push(u); // dfs存在的必要:统计同一棵树上的元素 
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(vis[v]==0)
            Dfs(v);
    }
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(degree,0,sizeof(degree));
        for(int i=1;i<=n;i++) //清零 
			mp[i].clear();
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d %d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
            degree[x]++;
            degree[y]++;
        }
        flag=0;
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                Dfs(i);
                int tmp=s.size();
                while(!s.empty())
                {
                    int u=s.front();
                    s.pop();
                    if(degree[u]!=tmp-1) flag=1;
                }
            }
        }
        if(flag==1) printf("NO\n");
        else printf("YES\n");
    }
}

<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、付费专栏及课程。

余额充值