易爆物(X-Plosives )基础并查集
复制代码
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 const int maxn = 100000 + 10; 5 int fa[maxn]; 6 7 int Find(int x){ 8 if (x == fa[x]) 9 return x; 10 else 11 return fa[x] = Find(fa[x]); 12 } 13 14 int main(){ 15 int a, b; 16 while (cin >> a){ 17 18 //init 19 for (int i = 0; i < maxn; i++) 20 fa[i] = i; 21 int ans = 0; 22 while (a != -1){ 23 cin >> b; 24 a = Find(a); 25 b = Find(b); 26 if (a == b) 27 ans++; 28 else 29 fa[b] = a; 30 cin >> a; 31 } 32 cout << ans << endl; 33 } 34 //system("pause"); 35 return 0; 36 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步