A1013
#include<iostream>
using namespace std;
const int maxn=500010;
int n,m,k;
struct node
{
int a,b;
}Node[maxn];
int p[maxn];
int find(int x)
{
if(p[x]!=x)
p[x]=find(p[x]);
return p[x];
}
int main()
{
cin >> n >> m >> k;
for(int i=0; i<m; i++)
{
cin >> Node[i].a >> Node[i].b;
}
while(k--)
{
int x;
cin >> x;
int cnt=n-1;
for(int i=1; i<=n; i++)
p[i]=i;
for(int i=0; i<m; i++)
{
int kk=Node[i].a,kk1=Node[i].b;
if(kk!=x && kk1!=x)
{
int pa=find(kk),pb=find(kk1);
if(pa!=pb)
{
p[pa]=pb;
cnt--;
}
}
}
cout << cnt-1 << endl;
}
return 0;
}
A1114
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=500010;
struct node
{
int a,b;
}e[maxn];
int hc[maxn],ha[maxn];
int p[maxn],c[maxn];
bool st[maxn]={false};
int find(int x)
{
if(p[x]!=x)
p[x]=find(p[x]);
return p[x];
}
struct Family
{
int id,c, hc,ha;
};
bool cmp(Family a,Family b)
{
if(a.ha*b.c!=b.ha*a.c)
{
return a.ha*b.c>b.ha*a.c;
}
return a.id<b.id;
}
int main()
{
int n;
cin >> n;
int m=0;
vector<Family> Node;
for(int i=0; i<n; i++)
{
int id,father,mother,k;
cin >> id >> father >> mother >> k;
st[id]=true;
if(father!=-1)
e[m++]={id,father};
if(mother!=-1)
e[m++]={id,mother};
for(int j=0; j<k; j++)
{
int son;
cin >> son;
e[m++]={id,son};
}
cin >> hc[id] >> ha[id];
}
for(int i=0; i<maxn; i++)
{
p[i]=i;
c[i]=1;
}
for (int i = 0; i < m; i ++ )
{
int a = e[i].a, b = e[i].b;
st[a] = st[b] = true;
int pa = find(a), pb = find(b);
if (pa != pb)
{
if (pb > pa) swap(pa, pb);
c[pb] += c[pa];
hc[pb] += hc[pa];
ha[pb] += ha[pa];
p[pa] = pb;
}
}
for(int i=0; i<maxn; i++)
{
if(st[i]==true && p[i]==i)
{
Node.push_back({i,c[i],hc[i],ha[i]});
}
}
cout << Node.size() << endl;
sort(Node.begin(),Node.end(),cmp);
for(int i=0; i<Node.size(); i++)
{
printf("%04d %d %.3lf %.3lf\n",Node[i].id,Node[i].c,(double)Node[i].hc/Node[i].c,(double)Node[i].ha/Node[i].c);
}
}
A1118
#include<iostream>
using namespace std;
const int maxn=10010;
struct node
{
int a,b;
}Node[maxn];
bool st[maxn];
int n;
int p[maxn];
int find(int x)
{
if(p[x]!=x)
p[x]=find(p[x]);
return p[x];
}
int main()
{
cin >> n;
int m=0;
int temp;
int niaoshu=0;
while(n--)
{
cin >> temp;
int temp2,daibiao;
for(int i=0; i<temp; i++)
{
cin >> temp2;
if(st[temp2]==false)
{
st[temp2]=true;
niaoshu++;
}
if(i==0)
daibiao=temp2;
if(i!=0)
Node[m++]={daibiao,temp2};
}
}
for(int i=1; i<=maxn; i++)
p[i]=i;
int cnt=niaoshu;
for(int i=0; i<m; i++)
{
int faA=find(Node[i].a),faB=find(Node[i].b);
if(faA!=faB)
{
p[faA]=faB;
cnt--;
}
}
cout << cnt << " " << niaoshu << endl;
int kk;
cin >> kk;
int wsf,xx;
while(kk--)
{
cin >> wsf >> xx;
if(find(wsf)==find(xx))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
A1107
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1010;
vector<int> xx[maxn];
int p[maxn];
int n;
int find(int x)
{
if(p[x]!=x)
p[x]=find(p[x]);
return p[x];
}
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
cin >> n;
int temp;
for(int T=1; T<=n; T++)
{
scanf("%d:",&temp);
int temp1;
for(int i=0; i<temp; i++)
{
cin >> temp1;
xx[temp1].push_back(T);
}
}
for(int i=1; i<=n
; i++)
p[i]=i;
for(int i=1; i<=1000; i++)
{
for(int j=1; j<xx[i].size(); j++)
{
int faA=find(xx[i][0]),faB=find(xx[i][j]);
if(faA!=faB)
p[faA]=faB;
}
}
int renshu[maxn]={0};
for(int i=1; i<=n; i++)
{
renshu[find(i)]++;
}
int ans=0;
for(int i=1; i<=n; i++)
{
if(renshu[i]!=0)
ans++;
}
cout << ans << endl;
sort(renshu+1,renshu+n+1,cmp);
for(int i=1; i<=ans; i++)
{
if(i!=1)
cout << " ";
cout << renshu[i];
}
}
A1048
#include<iostream>
#include<unordered_set>
using namespace std;
int main()
{
int n,m;
cin >> n >> m;
unordered_set<int> hash1;
int v1=1e9,v2;
for(int i=0; i<n; i++)
{
int x;
cin >> x;
int y=m-x;
if(hash1.count(y))
{
hash1.insert(x);
if(x>y)
swap(x,y);
if(v1>x)
{
v1=x;
v2=m-v1;
}
}
else
hash1.insert(x);
}
if(v1==1e9)
cout << "No Solution";
else
cout << v1 << " " << v2;
return 0;
}