Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- const int maxN = 10000;
- int nodeRootList[1 + maxN];
- /*struct node
- {
- int fatherNum;
- };
- node nodeRootList[1 + maxN];*/
- namespace algorithm
- {
- int findRoot(int thisNodeNum)
- {
- if(nodeRootList[thisNodeNum] == thisNodeNum)
- {
- return thisNodeNum;
- }
- else
- {
- nodeRootList[thisNodeNum] = findRoot(nodeRootList[thisNodeNum]);
- return nodeRootList[thisNodeNum];
- }
- }
- void mergeNodeRoot(int nodeNumI,int nodeNumJ)
- {
- int fatherI,fatherJ;
- fatherI = findRoot(nodeNumI);
- fatherJ = findRoot(nodeNumJ);
- if(fatherI != fatherJ)
- {
- nodeRootList[nodeNumJ] = nodeNumI;
- }
- }
- int solve()
- {
- int N,M;
- std::cin >> N >> M;
- for(int i = 1;i <= N;i++)
- {
- nodeRootList[i] = i;
- }
- for(int i = 1;i <= M;i++)
- {
- int Z,X,Y;
- std::cin >> Z >> X >> Y;
- if(Z == 1)
- {
- mergeNodeRoot(X,Y);
- }
- if(Z == 2)
- {
- if(findRoot[X] == findRoot[Y])
- {
- std::cout << "Y" << std::endl;
- }
- else
- {
- std::cout << "N" << std::endl;
- }
- }
- }
- }
- };
- int main()
- {
- algorithm::solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement