并查集维护连通块大小即可
#include<bits/stdc++.h>
using namespace std;
const int Maxn = 1e5+10;
int n, fa[Maxn], tot[Maxn], p;
int find(int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);}
int read(){
int x = 0;char c = getchar();
while(c > '9' || c < '0') c = getchar();
while(c <= '9' && c >= '0') {x = (x << 3) + (x << 1) + c - '0';c = getchar();}
return x;
}
int main(){
n = read();
for(int i = 1;i <= n;++ i) fa[i] = i,tot[i] = 1;
for(int i = 1;i <= n;++ i){
int l = find(i),r = find(read());
if(l != r){
fa[r] = l;
tot[l] = tot[r] = tot[l] + tot[r];
}
}
for(int i = 1;i <= n;++ i)
printf("%d ",tot[find(i)]);
return 0;
}
根据题意模拟即可,结构体里存储每位学生的信息,有 优化,所以敲的有点暴力
#include<bits/stdc++.h>
#define ri register
using namespace std;
const int Maxn = 1e5+10;
int n, m, idx;string s;
map <string,int> Map,SCR;
vector <pair<string,int> > v[Maxn];
vector <int> S;
bool vis[Maxn];
int cnt, sc[Maxn], tot;
string ans[Maxn], qk[Maxn];bool f;
int read(){
int x = 0;char c = getchar();
while(c > '9' || c < '0') c = getchar();
while(c <= '9' && c >= '0') {x = (x << 3) + (x << 1) + c - '0';c = getchar();}
return x;
}
void print(){
for(int i = 0;i < m;++ i)
cout << ans[i] << endl;
}
int main(){
scanf("%d%d",& n,& m);
for(ri int i = 1;i <= (n << 1);++ i){
cin >> s;
string c(s.substr(10,14));
if(!Map[c]) Map[c] = ++ idx;
v[Map[c]].push_back(make_pair(s, read()));
}
for(ri int i = 1;i <= m;++ i) S.push_back(read());
for(ri int i = 1;i <= idx;++ i){
cnt = 0;SCR.clear();
memset(vis, 0, sizeof(vis));memset(sc, 0, sizeof(sc));
for(ri int j = 0;j < v[i].size();++ j){
string t(v[i][j].first.substr(0,10));
if(!SCR[t]) SCR[t] = ++ cnt;
sc[SCR[t]] += v[i][j].second;
qk[SCR[t]] = t;
}
tot = 0;
for(ri int j = 1;j <= cnt;++ j){
sc[j] = ceil((double)sc[j] / 2);
f = 0;
for(ri int k = 0;k < m;++ k)
if(sc[j] == S[k] && !vis[k]) {++ tot;ans[k] = qk[j];vis[k] = f = 1;break;}
if(!f) break;
}
if(tot == m){print();return 0;}
}
return 0;
}
仍然是把每条路径分情况,子树间和根节点的答案统计,还有更新的顺序
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
template < typename Tp >
void read(Tp &x) {
x = 0; int fh = 1; char ch = 1;
while(ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if(ch == '-') fh = -1, ch = getchar();
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
x *= fh;
}
const int maxn = 100000 + 7;
const int maxm = 200000 + 7;
const int mod = 998244353;
int n, a[maxn], s[maxn];
int Head[maxn], to[maxm], Next[maxm], tot;
void add(int x, int y) {
to[++tot] = y, Next[tot] = Head[x], Head[x] = tot;
}
void Init(void) {
read(n);
for(int i = 1; i <= n; i++) read(a[i]);
for(int i = 1, x, y; i < n; i++) {
read(x); read(y);
add(x, y); add(y, x);
}
}
long long ans;
int cnt, zer[maxn], one[maxn];
void dfs(int x, int f) {
s[x] ^= s[f]; zer[x] = one[x] = 0;
for(int i = Head[x]; i; i = Next[i]) {
int t = to[i]; if(t == f) continue;
dfs(t, x);
if(s[x] ^ s[f]) {
cnt = (cnt + zer[t] * zer[x] % mod) % mod;
cnt = (cnt + one[t] * one[x] % mod) % mod;
}
else {
cnt = (cnt + zer[t] * one[x] % mod) % mod;
cnt = (cnt + one[t] * zer[x] % mod) % mod;
}
zer[x] += zer[t], one[x] += one[t];
}
if(s[x]) one[x]++;
else zer[x]++;
if(s[f]) cnt += zer[x];
else cnt += one[x];
cnt %= mod;
}
int solve(int id) {
for(int i = 1; i <= n; i++) {
s[i] = (a[i] >> id) & 1;
}
cnt = 0; dfs(1, 0);
return cnt;
}
void Work(void) {
for(int i = 0; i <= 15; i++) {
ans = (ans + solve(i) * (1ll << i) % mod) % mod;
}
printf("%lld\n", ans);
}
int main(void) {
Init();
Work();
return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = 1e15+100;
LL T, n, h, x, y, ans, a, b;
LL ABS(LL x){return x >= 0 ? x : -x;}
LL read(){
LL x = 0;char c = getchar();
while(c > '9' || c < '0') c = getchar();
while(c <= '9' && c >= '0') {x = (x << 3) + (x << 1) + c - '0';c = getchar();}
return x;
}
bool check(LL p,LL q,LL x,LL y){
if(ABS(x - p) % a != 0 || ABS(y - q) % b != 0) return 0;
LL t = ABS(x - p) / a,r = ABS(y - q) / b;
if(t < r) swap(t, r);
if(t != r && (t - r) & 1) return 0;
return 1;
}
LL work(LL p,LL q,LL x,LL y){
LL t = ABS(x - p) / a,r = ABS(y - q) / b;
return max(t, r);
}
int main(){
T = read();
while(T --){ans = INF;
n = read(),h = read(),x = read(),y = read(),a = read(),b = read();
if(check(1, 1, x, y)) ans = work(1, 1, x, y);
if(check(1, h, x, y)) ans = min(ans, work(1, h, x, y));
if(check(n, 1, x, y)) ans = min(ans, work(n, 1, x, y));
if(check(n, h, x, y)) ans = min(ans, work(n, h, x, y));
if(ans == INF) puts("-1");
else printf("%lld\n", ans);
}
return 0;
}
建树之后计算每个结点就好。每个结点会被遍历 次,总时间复杂度 .
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int Maxn = 1e6+10;
int n, siz[Maxn], fa[Maxn];
vector <int> e[Maxn];
LL ans;
int read(){
int x = 0;char c = getchar();
while(c > '9' || c < '0') c = getchar();
while(c <= '9' && c >= '0') {x = (x << 3) + (x << 1) + c - '0';c = getchar();}
return x;
}
void dfs(int u,int par){
siz[u] = 1;
for(int i = 0;i < e[u].size();++ i){
int v = e[u][i];
if(v != par)
dfs(v, fa[v] = u),siz[u] += siz[v];
}
}
int main(){
n = read();
for(int i = 1;i < n;++ i){
int u = read(),v = read();
e[u].push_back(v),e[v].push_back(u);
}
dfs(1, 0);
for(int i = 1;i <= n;++ i){
ans = 2ll * (n - 1) + 1 + 2ll * (n - siz[i]) * (siz[i] - 1);
for(int j = 0;j < e[i].size();++ j){
int v = e[i][j];if(v == fa[i]) continue;
ans += 1ll * siz[v] * (siz[i] - siz[v] - 1);
}
printf("%lld ", ans);
}
return 0;
}
不要在考前学习新的数据结构
确实少写了一个 ,写上就切了。
补上 之后的程序
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int Maxn = 1e5+10;
LL n, ans, rt, cnt;
int dist[Maxn],l[Maxn],r[Maxn],v[Maxn],t[Maxn],add[Maxn];
LL read(){
LL x = 0;char c = getchar();
while(c > '9' || c < '0') c = getchar();
while(c <= '9' && c >= '0') {x = (x << 3) + (x << 1) + c - '0';c = getchar();}
return x;
}
void pushDown(int x){
if(!add[x]) return;
if(l[x]){
v[l[x]] += add[x];
add[l[x]] += add[x];
}
if(r[x]){
v[r[x]] += add[x];
add[r[x]] += add[x];
}
add[x] = 0;
}
int merge(int x,int y){
if(!x || !y) return x | y;
if(v[x] > v[y]) swap(x, y);
pushDown(x),pushDown(y);
r[x] = merge(r[x], y);
if(dist[l[x]] < dist[r[x]]) swap(l[x], r[x]);
dist[x] = dist[r[x]] + 1;
return x;
}
int pop(int x){pushDown(x);return merge(l[x], r[x]);}
int main(){
n = read();dist[0] = -1;
for(int i = 1;i <= n;++ i) v[i] = read();
for(int i = 1;i <= n;++ i) t[i] = read();
for(int i = 1;i <= n;++ i){
if(!rt) rt = i;
else rt = merge(rt, i);ans = 0;
while(cnt < i && v[rt] <= t[i]) ans += v[rt],v[rt] = 0,++ cnt,rt = pop(rt);
ans += (i - cnt) * t[i];
printf("%lld ", ans);
if(!rt) continue;
v[rt] -= t[i];add[rt] -= t[i];pushDown(rt);
}
return 0;
}