当前位置:网站首页>"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;
}
边栏推荐
- Who can elaborate on the semi consistent read under mysqlrc and how to reduce the deadlock probability?
- nacos注册中心
- 请问为什么我进行mysql数据update时,kafka中采集到的是先删除原纪录(op d)再新增新
- The const keyword of ES6 declares variables
- 这个报错是什么鬼啊,不影响执行结果,但是在执行sql时一直报错。。。连接maxComputer是使用
- VScode连接ssh遇到的问题
- 安装postgis时报找不到“POSTGIS_VERSION”这个函数
- 数据挖掘——关联分析基础介绍(上)
- UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in range(128)
- Ssl== certificate related concepts
猜你喜欢

Nacos registry

【BGP】小型实验

The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout

Ssl== certificate related concepts

Is the browser multi process or single process?

1985-2020 (8 Editions) global surface coverage download and introduction

Typescript from getting started to mastering (XXII) namespace namespace (I)

LDP --- 标签分发协议

Design of environment detection system based on STM32 and Alibaba cloud

Value transmission and address transmission of C language, pointer of pointer
随机推荐
Use case of arrow function of new features in ES6
这个报错是什么鬼啊,不影响执行结果,但是在执行sql时一直报错。。。连接maxComputer是使用
安装ros的laser_scan_matche库所遇到的问题(一)
The table of antd hides the pager when there is only one page
伏英娜:元宇宙就是新一代互联网!
[kvm] create virtual machine from kickstart file
通过PWM呼吸灯和PWM控制直流电机来详细介绍TIM的输出比较功能
When defining an array, the size must be constant
Ma Zhixing entered the mass production of front loading, starting with the self-developed domain controller?
Typescript from getting started to mastering (XVI) configuration file - first knowledge of compileroptions configuration item
路西法98-生活记录ing
C declaration and initialization and assignment
大佬们flink的JDBC SQL Connector现在不支持所有的数据库吗,例如vertica?
UCOS任务切换过程
SQL窗口函数
数据集成这个地方的过滤条件该咋写,用的啥语法?sql语法处理bizdate可以不
Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation
力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)
VScode连接ssh遇到的问题
Data mining -- Introduction to the basis of association analysis (Part 1)