当前位置:网站首页>【补题日记】[2022牛客暑期多校1]J-Serval and Essay
【补题日记】[2022牛客暑期多校1]J-Serval and Essay
2022-07-24 02:23:00 【cls1277】
Pro
https://ac.nowcoder.com/acm/contest/33186/J
Sol
如果一个点可以被染色,当且仅当只有一条入边连向该点。因此如果将入度为1的点预先加入队列,使用类似拓扑的方式对所连节点进行更新,同时更新队列,并用并查集维护所有可以同时被染黑的点。
可以发现最终并查集的形成了以 f a [ i ] = i fa[i]=i fa[i]=i为根的树组成的森林,同一棵树内满足可以同时染色,答案即为森林中树的大小的最大值 。
牛客的机子还是不太稳定,所以下面的代码里加了一点卡常的东西
Code
//By cls1277
#include<bits/stdc++.h>
using namespace std;
typedef int LL;
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define endl '\n'
const LL maxn = 2e5+5;
LL n, fa[maxn];
inline LL read() {
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-') f=-1; ch=getchar(); }
while(ch>='0'&&ch<='9') {
x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
return x*f;
}
LL find(LL x) {
while(x!=fa[x]) x = fa[x] = fa[fa[x]];
return x;
}
unordered_set<LL> e[maxn], pre[maxn];
int main() {
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
#ifdef DEBUG
freopen("data.txt","r",stdin);
#endif
LL t; t=read(); //cin>>t;
Fo(tt,1,t) {
// cin>>n;
n = read();
vector<LL> sz(n+1, 1);
vector<bool> vis(n+1, 0);
// unordered_map<LL, unordered_set<LL>> e, pre;
Fo(i,1,n) {
e[i].clear();
pre[i].clear();
}
Fo(i,1,n) {
fa[i] = i;
LL x; x=read(); //cin>>x;
Fo(j,1,x) {
LL y; y=read(); //cin>>y;
e[y].insert(i);
pre[i].insert(y);
}
}
// queue<LL> q;
vector<LL> q(n+1, 0);
LL _h=0, _t=-1;
Fo(i,1,n)
if(pre[i].size()==1) {
// q.push(i);
q[++_t] = i;
vis[i] = 1;
}
while(_h<=_t) {
// while(!q.empty()) {
// LL u = q.front(); q.pop();
LL u = q[_h++];
LL p = *pre[u].begin();
// for(auto it:pre[u]) {
// p = it;
// break;
// }
LL p1=find(u), p2=find(p);
if(p1==p2) continue;
if(sz[p1]>sz[p2]) swap(p1, p2);
fa[p1] = p2;
sz[p2] += sz[p1];
for(auto it:e[p1]) {
e[p2].insert(it);
pre[it].erase(p1);
pre[it].insert(p2);
if(pre[it].size()==1&&!vis[it]) {
vis[it] = 1;
// q.push(it);
q[++_t] = it;
}
}
}
LL ans = 1;
Fo(i,1,n) ans = max(ans, sz[i]);
// cout<<"Case #"<<tt<<": "<<ans<<endl;
printf("Case #%lld: %lld\n", tt, ans) ;
}
return 0;
}
边栏推荐
- 5年接觸近百比特老板,身為獵頭的我,發現昇職的秘密不過4個字
- Reconnaître le Protocole de couche de transport - TCP / UDP
- LoadRunner12安装、录制第一个脚本以及代理服务器没有响应解决
- [重要通知]星球线上培训第三期来袭!讲解如何在QTYX上构建自己的量化策略!...
- Build a CPU Simulator
- What's new in the ranking list in July? This language is invincible?
- C language curriculum - personal information management system (including student grades and consumption records)
- Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
- Enter cnpm -v and cnpm appears: the file c:\users\19457\appdata\roaming\npm\cnpm.ps1 cannot be loaded because running scripts is prohibited on this system.
- Resumption: a deck of cards (54), three people fighting the landlord, what is the probability that the big and small kings are in the same family
猜你喜欢

After five years of contact with nearly 100 bosses, as a headhunter, I found that the secret of promotion was only four words

Running around, market and quantitative page function optimization! Stock quantitative analysis tool qtyx-v2.4.5

杂志特稿:元宇宙将重塑我们的生活,我们要确保它变得更好

Implementation of POP3 client code

分布式资源管理与任务调度框架Yarn

Give me five minutes, give you a "cloud"

ASP. Net core write a cache attribute tool

网络协议详解 :UDP

新红包封面平台可搭建分站独立后台的源码

pbootcms模板调用标签序数从2开始或者自动数开始
随机推荐
深入了解-微信开发者工具
Use of component El scrollbar
[C language] preprocessing details
快速排序注意点
关于缺少编程基础的朋友想转行 ABAP 开发岗提出的一些咨询问题和解答
Hydrogen entrepreneurship competition | Liu Xiaoqi, chairman of Guohua Investment: take advantage of the integration of scenery, hydrogen storage and finance to host the entrepreneurship competition a
Deliver temperature with science and technology, vivo protects the beauty of biodiversity
Phpcms realizes product multi condition screening function
餐饮连锁门店重塑增长背后的数字化转型
什么叫裸写SQL?express操作mysql用什么中件间或插件好呢?
小散量化炒股记|基于多任务爬虫技术, 实现A股实时行情Level1采样
组件el-scrollbar的使用
BPG notes (III)
Detailed comparison between graphic array and linked list, performance test
Wallys/PD-60 802.3AT Input Output802.3AT/AT 85% Efficiency 10/100/1000M GE Surge Protection
College degree want to 0 basic programming after looking for a job feasible?
深入理解微信小程序的底层框架(二)组件系统、Exparser
How to do a good job of accompanying translation
Loadrunner12 installation, recording the first script and the proxy server did not respond to the solution
浅谈元宇宙中DeFi的可能性和局限性