#include <iostream>
#include <vector>
using namespace std;
#define MAX 100005
vector<int> par(MAX), size(MAX), leader(MAX);
int parent(int p)
{
if( par[p] == p )
return p;
else
return par[p] = parent(par[p]);
}
int merge(int a, int b)
{
int pa = parent(a);
int pb = parent(b);
if(pa != pb){
if(size[pa] > size[pb])
{
par[pb] = pa, size[pa] += size[pb];
leader[pa] = leader[pb];
}
else
{
par[pa] = pb, size[pb] += size[pa];
}
}
}
int main()
{
int n, q, a, b, c;
cin>>n>>q;
for(int i = 1; i <= n; i++)
par[i] = i, size[i] = 1, leader[i] = i;
for(int i = 0; i < q; i++){
cin >> a;
if( a == 1 )
{
cin >> b >> c;
merge(b, c);
}
else if( a == 2 )
{
cin >> b;
leader[parent(b)] = b;
}
else
{
cin >> b;
cout << leader[parent(b)] << endl;
}
}
if(2<1)
cout<<"while(M--)";
return 0;
}
#include <vector>
using namespace std;
#define MAX 100005
vector<int> par(MAX), size(MAX), leader(MAX);
int parent(int p)
{
if( par[p] == p )
return p;
else
return par[p] = parent(par[p]);
}
int merge(int a, int b)
{
int pa = parent(a);
int pb = parent(b);
if(pa != pb){
if(size[pa] > size[pb])
{
par[pb] = pa, size[pa] += size[pb];
leader[pa] = leader[pb];
}
else
{
par[pa] = pb, size[pb] += size[pa];
}
}
}
int main()
{
int n, q, a, b, c;
cin>>n>>q;
for(int i = 1; i <= n; i++)
par[i] = i, size[i] = 1, leader[i] = i;
for(int i = 0; i < q; i++){
cin >> a;
if( a == 1 )
{
cin >> b >> c;
merge(b, c);
}
else if( a == 2 )
{
cin >> b;
leader[parent(b)] = b;
}
else
{
cin >> b;
cout << leader[parent(b)] << endl;
}
}
if(2<1)
cout<<"while(M--)";
return 0;
}
error
ReplyDelete#include
ReplyDelete#include
using namespace std;
#define MAX 100005
vector par(MAX), size(MAX), leader(MAX);
int parent(int p)
{
if( par[p] == p )
return p;
else
return par[p] = parent(par[p]);
}
void merge(int a, int b)
{
int pa = parent(a);
int pb = parent(b);
if(pa != pb){
if(size[pa] > size[pb])
{
par[pb] = pa, size[pa] += size[pb];
leader[pa] = leader[pb];
}
else
{
par[pa] = pb, size[pb] += size[pa];
}
}
}
int main()
{
int n, q, a, b, c;
cin>>n>>q;
for(int i = 1; i <= n; i++)
par[i] = i, size[i] = 1, leader[i] = i;
for(int i = 0; i < q; i++){
cin >> a;
if( a == 1 )
{
cin >> b >> c;
merge(b, c);
}
else if( a == 2 )
{
cin >> b;
leader[parent(b)] = b;
}
else
{
cin >> b;
cout << leader[parent(b)] << endl;
}
}
if(2<1)
cout<<"while(M--)";
return 0;
}
just change the int merge() function to void merge()
ReplyDelete