SDNU 1351.the Number of Department(并查集)
Description
Company A has N employees, this company has lots of departments around the country. The employees who belong to different departments do not know each other, but if two employees are working in the same department they will know each other directly or indirectly (For example, 1 and 2 know each other directly,2 and 3 know each other directly, so 1 and 3 know each other indirectly, and they belong to the same department).
These N employees are numbered from 1 to N.
Now, giving you M informations about these N employees (two employees' number, who knows each other directly).
If any people did not appear in the list of the M informations, these people belong to different departments.
Now, you should calculate how many departments this company has.
These N employees are numbered from 1 to N.
Now, giving you M informations about these N employees (two employees' number, who knows each other directly).
If any people did not appear in the list of the M informations, these people belong to different departments.
Now, you should calculate how many departments this company has.
Input
The 1st line, N, ( 1<=N<=10000 ) the number of the employees belong to the company.
The 2ed line, M, ( 1<=M<=100000 ) the number of the informations about these N employees.
The next M lines, each line has two numbers, i and j, said that employee i and j knows each other directly.
The 2ed line, M, ( 1<=M<=100000 ) the number of the informations about these N employees.
The next M lines, each line has two numbers, i and j, said that employee i and j knows each other directly.
Output
The maximum number of the departments that belong to company A.
Sample Input
11 9 1 2 4 5 3 4 1 3 5 6 7 10 5 10 6 10 8 9
Sample Output
3
Hint
Source
Unknown
思路:一开始在纠结到底是拓扑排序还是并查集。如果是拓扑排序的话,靠入度不好判断有几个部门。所以,应该是并查集...
#include <cstdio> #include <iostream> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <map> using namespace std; #define ll long long const int inf = 0x3f3f3f3f; const int mod = 1e9+7; int f[100000+8], n, m, sum; void init() { for(int i = 1; i <= n; i++) f[i] = i; } int get(int v) { if(f[v] == v)return v; else { f[v] = get(f[v]); return f[v]; } } int merg(int x, int y) { int t1 = get(x); int t2 = get(y); if(t1 != t2)///判断是否是直接或间接认识(判断是否为一个祖先) f[t2] = t1;///靠左原则,且路径压缩后根植也改变了 return 0; } int main() { scanf("%d%d", &n, &m); init(); int a, b; for(int i = 0; i<m; i++) { scanf("%d%d", &a, &b); merg(a, b); } for(int i = 1; i <= n; i++) if(f[i] == i)sum++; // for(int i = 1; i <= n; i++)cout<<"i = "<<i<<"---"<<f[i]<<endl; printf("%d\n", sum); return 0; }
分类:
并查集
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· 程序员转型AI:行业分析
· 为DeepSeek添加本地知识库
· 深入集成:使用 DeepSeek SDK for .NET 实现自然语言处理功能
· .NET程序员AI开发基座:Microsoft.Extensions.AI