首页 > 其他分享 >CF1795F Blocking Chips

CF1795F Blocking Chips

时间:2023-12-31 11:22:06浏览次数:32  
标签:fir cnt return int res CF1795F include Chips Blocking

题意

给定一棵大小为 \(n\) 的树,有 \(k\) 个人,第 \(i\) 个人在节点 \(a_i\)。

从第 \(1\) 秒开始,依次操作第 \(1, 2, 3, \ldots, k, 1, 2, 3, \ldots, k, \ldots, k, \ldots\) 个人,把这个人移动到没有走过的点。

Sol

调了 \(3h\),给哥们整吐了。

不难想到二分答案时间,算出每个人走了多少步。

然后考虑如何 \(check\)。

记 \(f_i\) 表示当前节点往下还可以走多远。

\(g_i\) 表示当前节点需要往上走多远。

不难发现:\(f_x = \max f_v + 1\)。

注意到如果有多个节点需要往上走,或者当前节点也需要走,那么不合法。

把这几个条件判完就过了。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <bitset>
#include <assert.h>
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
const int N = 2e5 + 5, M = 4e5 + 5;

namespace G {

array <int, N> fir;
array <int, M> nex, to;
int cnt = 1;
void add(int x, int y) {
	cnt++;
	nex[cnt] = fir[x];
	to[cnt] = y;
	fir[x] = cnt;
}

}

bitset <N> vis;

array <int, N> dis, f, g;

bool dfs(int x, int fa) {
	int res = -1;
	if (vis[x])
		res = dis[x];
	for (int i = G::fir[x]; i; i = G::nex[i]) {
		if (G::to[i] == fa) continue;
		bool tp = dfs(G::to[i], x);
		if (!tp) return tp;
		if (!vis[G::to[i]]) {
			f[x] = max(f[x], f[G::to[i]] + 1);
			continue;
		}
		if (~res && g[G::to[i]]) return false;
		else if (g[G::to[i]]) res = g[G::to[i]] - 1;
	}
	if (!~res) return true;
	vis[x] = 1;
	if (res > f[x]) g[x] = res;
	else g[x] = 0;
	return (x == 1 ? !g[x] : 1);
}

array <int, N> idx;

bool check(int x, int n, int k) {
	for (int i = 1; i <= n; i++)
		dis[i] = vis[i] = f[i] = g[i] = 0;
	for (int i = 1; i <= k; i++)
		dis[idx[i]] = x / k, vis[idx[i]] = 1;
	for (int i = 1; i <= x % k; i++)
		dis[idx[i]]++;
	return dfs(1, 0);
}


void solve() {
	int n = read();
	for (int i = 1; i <= n; i++)
		G::fir[i] = 0;
	G::cnt = 1;
	for (int i = 2; i <= n; i++) {
		int x = read(), y = read();
		G::add(x, y), G::add(y, x);
	}
	int k = read();
	for (int i = 1; i <= k; i++)
		idx[i] = read();
	int l = 0, r = n + 1;
	int ans = 0;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (check(mid, n, k)) ans = mid, l = mid + 1;
		else r = mid - 1;
	}
	write(ans), puts("");
}
int main() {
	int T = read();
	while (T--) solve();
	return 0;
}

标签:fir,cnt,return,int,res,CF1795F,include,Chips,Blocking
From: https://www.cnblogs.com/cxqghzj/p/17937321

相关文章

  • CompletableFuture + LinkedBlockingDeque 实现生产者消费者案例
    设计要求:1.设计一个生产者生产,消费者消费场景;2.使用线程池 CompletableFuture+队列LinkedBlockingDeque实现;3.生产者生产的数据存储到长度为5的LinkedBlockingDeque队列,消费者消费从LinkedBlockingDeque队列中取数据;4.生产者和消费者均是多线程且不知道谁快谁慢,互......
  • PriorityBlockingQueue 优先级队列
    packagestudy;importlombok.Data;importjava.util.Comparator;importjava.util.concurrent.PriorityBlockingQueue;publicclassPriorityBlockingQueueDemo{publicstaticvoidmain(String[]args)throwsInterruptedException{PriorityBlocki......
  • 阻塞队列之 LinkedBlockingQueue
    LinkedBlockingQueue:Java多线程编程中的阻塞队列在Java多线程编程中,LinkedBlockingQueue是一个非常重要的类,它提供了一种用于在生产者和消费者之间进行数据传递的机制。LinkedBlockingQueue广泛应用于各种场景,如线程池、任务队列等。本文将详细介绍LinkedBlockingQueue的原理......
  • PostgreSQL - Check blocking SQL statements
    pg_locksviewLookingat pg_locks showsyouwhatlocksaregrantedandwhatprocessesarewaitingforlockstobeacquired.Agoodquerytostartlookingforlockproblems:selectrelation::regclass,*frompg_lockswherenotgranted;pg_stat_activit......
  • BlockingQueue阻塞队列
    BlockingQueue阻塞队列BlockingQueue简介juc包下,BlockingQueue很好的解决了多线程中,高效安全的"传输数据"问题。阻塞队列,是一个队列,可以是数据从队列的一端输入,从另一端输出。当队列空时,从队列获取元素线程被阻塞,直到其他线程向空的队列插入新元素。当队列满时,向队列添加元......
  • hci0 command 0xfc20 tx timeout(Realtek 8761B Chipset, Bluetooth 5.0)
    当前使用的Linux内核版本: 4.4.189插上USBBluetooth5.0Adapter后,dmesg显示如下log:[240.348480]usb3-1.2:newfull-speedUSBdevicenumber6usingehci-platform[240.437834]usb3-1.2:NewUSBdevicefound,idVendor=0bda,idProduct=8771[240.438541]us......
  • BlockingQueue队列详解
    /**本例介绍一个特殊的队列:BlockingQueue,如果BlockQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态,直到BlockingQueue进了东西才会被唤醒.同样,如果BlockingQueue是满的,任何试图往里存东西的操作也会被阻断进入等待状态,直到BlockingQueue里有空间才会被......
  • java IO模型:BIO(Blocking I/O)
    BIO属于同步阻塞IO模型。同步阻塞IO模型中,应用程序发起read调用后,会一直阻塞,直到内核把数据拷贝到用户空间。在客户端连接数量不高的情况下,是没问题的。但是,当面对十万甚至百万级连接的时候,传统的BIO模型是无能为力的。因此,我们需要一种更高效的I/O处理模型来应对更高......
  • BlockingQueue---SynchronousQueue
    概述A{@linkplainBlockingQueueblockingqueue}inwhicheachinsertoperationmustwaitforacorrespondingremoveoperationbyanotherthread,andviceversa.Asynchronousqueuedoesnothaveanyinternalcapacity,notevenacapacityofone.......
  • Comparing QCN9274 and QCN9074: WPA3 Support in Qualcomm Wi-Fi Chips
    EnhancingIndustrialWirelessSecuritywithWPA3-CapableRouterBoardsandNetworkCardsIntoday'sindustriallandscape,wirelessconnectivityplaysapivotalroleinoptimizingoperations,monitoringequipment,andfacilitatingdataexchange.Howeve......