当前位置:网站首页>"Weilai Cup" 2022 Niuke summer multi school training camp 1 J serval and essay (heuristic merger)
"Weilai Cup" 2022 Niuke summer multi school training camp 1 J serval and essay (heuristic merger)
2022-07-29 04:05:00 【Snow_ raw】
" Weilai cup "2022 Niuke summer multi school training camp 1 J Serval and Essay( Heuristic merging )
The question : Given a acyclic and multiplicity free edge graph , All initial points are white , You can choose any point to make it black , The premise that other white dots can be polluted by black dots is All the edges are black dots . Ask the maximum number of black dots in the figure .
Ideas : Consider merging sets , Heuristic merging . If 1 1 1 Point dyeing black on No. 1 can make 2 2 2 The number point turns black , 2 2 2 Turning black on the number can make 3 3 3 The number point turns black . Then we can directly choose to 1 1 1 Dot dyeing , 1 、 2 、 3 1、2、3 1、2、3 Points are in a set . We just need to constantly merge the two sets ( Father gather and son gather ), If the only entry of the son's collection is the father's collection . take s i z e ( ) size() size() Small sets merge into s i z e ( ) size() size() In a large collection . The sum O ( ( n + m ) l o g 2 n ) O((n+m)log^2n) O((n+m)log2n) .
Code :
#include<bits/stdc++.h>
using namespace std;
#define int long long
#pragma GCC optimize(3)
#define re register int
typedef pair<int,int>PII;
#define pb emplace_back
#define debug(a) cout<<a<<' ';
#define fer(i,a,b) for(re i=a;i<=b;i++)
#define der(i,a,b) for(re i=a;i>=b;i--)
int n;
const int N = 2e5+10;
set<int>in[N],out[N];
int p[N],si[N];
int cnt;
int find(int x){
if(p[x]!=x)p[x]=find(p[x]);
return p[x];
}
void merge(int u){
int fa=*in[u].begin();
u=find(u);
fa=find(fa);
if(u==fa)return ;
if(out[u].size()>out[fa].size()){
swap(u,fa);
}
si[fa]+=si[u];
p[u]=fa;
vector<int>vec;
for(auto x:out[u]){
out[fa].insert(x);
in[x].erase(u);
in[x].insert(fa);
if(in[x].size()==1)vec.push_back(x);
}
for(auto x:vec)merge(x);
}
void cf(){
cin>>n;
for(int i=1;i<=n;i++)p[i]=i,si[i]=1;
for(int i=1;i<=n;i++){
int m;
cin>>m;
for(int j=1;j<=m;j++){
int x;
cin>>x;
in[i].insert(x);
out[x].insert(i);
}
}
for(int i=1;i<=n;i++){
if(in[i].size()==1){
merge(i);
}
}
int ma=0;
for(int i=1;i<=n;i++){
int x=p[i];
ma=max(ma,si[x]);
}
for(int i=1;i<=n;i++){
in[i].clear();
out[i].clear();
}
cout<<"Case #"<<++cnt<<": "<<ma<<endl;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int _=1;
cin>>_;
while(_--){
cf();
}
return 0;
}
边栏推荐
猜你喜欢

Lvs+keepalived high availability deployment practical application

力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)

Some problems about pointers

Value transmission and address transmission of C language, pointer of pointer

SQL窗口函数

华为天才少年稚晖君做了一把模块化机械键盘,引起极客圈地震,网友:这才是真正的客制化...

Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation
![[kvm] create virtual machine from kickstart file](/img/0e/292ccb6862e29d948ad6ece86b7945.png)
[kvm] create virtual machine from kickstart file

Function pointer and callback function

Ma Zhixing entered the mass production of front loading, starting with the self-developed domain controller?
随机推荐
Routing knowledge
Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds
Since 2019, you must have stopped using this marketing strategy
Three tier architecture of enterprise network
JS realizes the function of one click Copy
Analysis of new retail o2o e-commerce model
MySQL Part 3
有一种密码学专用语言叫做ASN.1
请问,在sql client中,执行insert into select from job时,如何单
MySQL第四篇(完结)
安装ros的laser_scan_matche库所遇到的问题(一)
企业网的三层架构
AssertionError(“Torch not compiled with CUDA enabled“)
First knowledge of C language (3)
When array is used as a function parameter, it is better to use the array size as a function parameter
Problems encountered in vscode connection SSH
Typescript from getting started to mastering (XXIII) namespace namespace (Part 2)
There is a special cryptology language called asn.1
Pat a1069/b1019 the black hole of numbers
[principle] several ways of horizontal penetration