文章目录
2020牛客暑期多校训练营(第八场)题解及补题
比赛过程
这场打的罚时太高了,一开始没有想到签到题就需要用算法,一直在想贪心策略,冷静下来想用并查集和拓扑排序做了,K题一开始思路没有问题,但是有一些小细节错误和没有及时注意到暴longlong了,导致wa的太多,顺便学习了__int128的用法。G题数据量看似 O ( n 3 ) O(n^3) O(n3) 不可行,其实跑不满,直接莽就可以了,不过在比赛的时候还是把 o ( n 2 ) o(n^2) o(n2) 的做法写出来了。
题解
(没过/没补的题空着不管,过了/补了的题目写题意/解法/代码)
(如果要写数学式子用 LaTeX \LaTeX LATEX,比如表达式 x 2 + 2 x + 1 x^2+2x+1 x2+2x+1)
A
题意
有 n n n 个球员, m m m 个粉丝。给出每个球员的所有粉丝。
如果一个人是这个球员的粉丝,那么这个人喜欢看这个球员。
如果A和B都喜欢看同一个球员,那么如果B喜欢看的球员A也喜欢看。
问最少需要多少球员才能让每一个粉丝都愿意看,即至少有一个喜欢看的球员入选。有 q q q 次粉丝关系的修改,修改完回答询问。
解法
事实上这题就是求连通块的个数,如果有球迷的度为 0 0 0,那么答案为-1,否则答案为连通块个数-孤立球员个数。使用可撤销并查集来维护。记录每一条边的存在时间,使用线段树来维护边的存在时间。
可撤销并查集不可以路径压缩,所以需要按秩合并,用栈来保存,撤销的时候还原祖先结点。
代码
#pragma region
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define tr t[root]
#define lson t[root << 1]
#define rson t[root << 1 | 1]
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
//fread->R
bool IOerror = 0;
//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
inline char nc() {
static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
if (p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, BUF_SIZE, stdin);
if (pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <class T>
inline bool R(T &x) {
bool sign = 0;
char ch = nc();
x = 0;
for (; blank(ch); ch = nc())
;
if (IOerror) return false;
if (ch == '-') sign = 1, ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
if (sign) x = -x;
return true;
}
inline bool R(double &x) {
bool sign = 0;
char ch = nc();
x = 0;
for (; blank(ch); ch = nc())
;
if (IOerror) return false;
if (ch == '-') sign = 1, ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc()) x = x * 10 + ch - '0';
if (ch == '.') {
double tmp = 1;
ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc())
tmp /= 10.0, x += tmp * (ch - '0');
}
if (sign)
x = -x;
return true;
}
inline bool R(char *s) {
char ch = nc();
for (; blank(ch); ch = nc())
;
if (IOerror)
return false;
for (; !blank(ch) && !IOerror; ch = nc())
*s++ = ch;
*s = 0;
return true;
}
inline bool R(char &c) {
c = nc();
if (IOerror) {
c = -1;
return false;
}
return true;
}
template <class T, class... U>
bool R(T &h, U &... t) {
return R(h) && R(t...); }
#undef OUT_SIZE
#undef BUF_SIZE
}; // namespace fastIO
using namespace fastIO;
template <class T>
void _W(const T &x) {
cout << x; }
void _W(const int &x) {
printf("%d", x); }
void _W(const int64_t &x) {
printf("%lld", x); }
void _W(const double &x) {
printf("%.16f", x); }
void _W(const char &x) {
putchar(x); }
void _W(const char *x) {
printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) {
_W(x.F), putchar(' '), _W(x.S); }
template <class T>
void _W(const vector<T> &x) {
for (auto i = x.begin(); i != x.end(); _W(*i++))
if (i != x.cbegin()) putchar(' ');
}
void W() {
}
template <class T, class... U>
void W(const T &head, const U &... tail) {
_W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
#pragma endregion
const int maxn = 4e5 + 10;
map<int, int> mp[maxn];
int n, m, q, cnt, cnta, cntb;
struct con {
int a, b;
};
struct recv {
int a, asz, b, bsz;
int cnt, cnta, cntb;
};
struct node {
int l, r;
vector<con> e;
vector<recv> rec;
} t[maxn << 2];
int f[maxn], sz[maxn], ans[maxn];
inline void build(int root, int l, int r) {
tr.l = l, tr.r = r, tr.e.clear(), tr.rec.clear();
if (l == r) return;
int mid = (l + r) >> 1;
build(root << 1, l, mid);
build(root << 1 | 1, mid + 1, r);
}
inline void add(int root, int l, int r, con d) {
if (l <= tr.l && tr.r <= r) {
tr.e.push_back(d);
return;
}
int mid = (tr.l + tr.r) >> 1