1006.Maex
题意:
给定一棵由从 1 到 n 编号的 n 个顶点组成的有根树,根为顶点 1。顶点 i 有一个自然数权重 ai,并且没有两个不同的顶点具有相同的权重。bu是以u为根的子树的权重的MEX,ai未知,求最大的bi和。
题解:
用sz[u]表示u子树节点个数, 表示dp[u]子树内sum(bi)可以达到的最大值。
那么考虑转移,u本身的b[u]一定可以是sz[u],而因为a必须两两不同,0只能在u的某个子树内,所以除了某个子树,其他子树节点的bi一定都为0.
那么转移就是:dp[u] = sz[u] +max(dp[v])
代码:
#include<iostream>
#include<set>
#include<string>
#include<vector>
#include<cstring>
#include<queue>
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int N = 1e6 + 10;
int n;
vector<int> g[N];
int siz[N], dp[N];
void dfs(int u, int fr){
siz[u] = 1, dp[u] = 0;
for(auto v : g[u]){
if(v == fr) continue;
dfs(v, u);
siz[u] += siz[v];
dp[u] = max(dp[u], dp[v]);
}
dp[u] += siz[u];
}
void solve(){
cin >> n;
for(int i = 0; i < n; i++) g[i].clear();
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
u --, v --;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0, -1);
cout << dp[0] << '\n';
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t--){
solve();
}
}
1012.Loop
题意:
给定一个数列,在指定操作数内,可以选择一段区间,将区间内的数字左移一位,最左边的数字移到最右边,问最大字典序的结果。
题解:
每次贪心地将第一个ai < ai+1的数放到第一个小于它的数前面。
代码:
#include<iostream>
#include<set>
#include<string>
#include<vector>
#include<cstring>
#include<queue>
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int N = 1e6 + 10;
int n, k;
int a[N], b[N], ans[N];
void solve(){
cin >> n >> k;
for(int i = 1; i <= n ;i++) cin >> a[i];
int tot = 0, num = 0;
priority_queue<int> q;
for(int i = 1; i <= n; i++){
while(tot && b[tot] < a[i] && k){
k --;
q.push(b[tot]);
tot --;
}
b[++tot] = a[i];
}
for(int i = 1, p = 1; i <= n; i++){
if(p > tot){
ans[++num] = q.top();
q.pop();
}
else if(q.empty()){
ans[++num] = b[p++];
}
else{
if(b[p] < q.top()){
ans[++num] = q.top();
q.pop();
}
else ans[++num] = b[p++];
}
}
for(int i = 1;i <= n; i++) cout << ans[i] << (i == n ? '\n' : ' ');
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t--){
solve();
}
}
1009.Map
题意:
给定一张大地图和小地图,两张贴在一起,问重合点的坐标。
题解:
用向量,相似三角形和阿波罗尼斯圆都可以轻松解决。
代码:
#include<iostream>
#include<cmath>
#include<iomanip>
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int N = 1e6 + 10;
double k;
int x[9], y[9];
double dis(int i, int j)
{
return sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
}
void solve(){
for(int i = 1; i <= 8; ++i)
cin >> x[i] >> y[i];
k=dis(1, 2) / dis(5, 6);
double A = (x[4] - x[1]) / dis(1, 4) - (x[8] - x[5]) / k / dis(5, 8);
double B = (x[2] - x[1]) / dis(1,2) - (x[6] - x[5]) / k / dis(5, 6);
double C = x[5] - x[1];
double D = (y[4] - y[1]) / dis(1, 4) - (y[8] - y[5]) / k / dis(5, 8);
double E = (y[2] - y[1]) / dis(1, 2) - (y[6] - y[5]) / k / dis(5, 6);
double F = y[5] - y[1];
double ansx = (B * F - C * E) / (B * D - A * E);
double ansy = (C * D - A * F) / (B * D - A * E);
cout << x[1] + (x[2] - x[1]) * ansy / dis(1, 2) + (x[4] - x[1]) * ansx / dis(1, 4) << " ";
cout << y[1] + (y[2] - y[1]) * ansy / dis(1, 2) + (y[4] - y[1]) * ansx / dis(1, 4) << '\n';
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(9);
int t;
cin >> t;
while(t--){
solve();
}
}
1010.Planar Graph
题意:
给定一张图,问最少需要去掉几条边,使图只有一个面,并给出删掉的边。
题解:
本题的题目背景是平面图欧拉定理的一种证明方式. 根据平面图欧拉定理V - E + F = k + 1 (V,E,F,k 分别为点, 边, 面, 连通分量的个数), 每去掉一条边(加一个隧道可以理解为删除一条边), 平面图G的面的个数就会增加 . 因此最终边的个数为V + 1 - k - 1 = V - k, 因此删边个数为E - V + k. 于是对于每个连通分量, 保留了V' - 1条边(V'为该连通分量的点数). 如果该连通分量有圈, 那么这个圈内的区域一定不与外部的无穷平面连通, 不符合题意. 因此每个连通分量都不含圈, 且有V' - 1条边, 因此每个连通分量都变成树.
现在从另外一个角度考虑. 我们构造一个新图G'. 我们把平面图的每个面当作一个点, 如果两个面被平面图的某条边分隔, 那么这两个面在 对应的点之间连一条边(于是G和G'的边是一一对应关系).G'的点个数为F = k + 1 - V + E,G的删边个数为E - V +k = F - 1, 由于建隧道之后任意两个区域可达, 因此如果把G中删掉的边在G'中考虑, 删除G'中在G保留的边, 那么边个数为F - 1, 且任意两点连通. 因此这些边构成G'的生成树.
于是, 问题转化为求G'的字典序最小的生成树. 由于每条边边权不同, 因此字典序最小的生成树等价于G'最小生成树, 等价于G的生成树边权和最小(考虑kruskal算法的构造过程, 并注意所有边权不同时最小生成树唯一). 由于所有边边权和为定值, 因此G'生成树边权和最小等价于G的每个连通分量对应的树边权和最大. 于是只需要对于G的每个连通分量求出最大生成树, 然后这些最大生成树没有用到的边就是G'的最小生成树的边.
代码:
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int N = 1e5 + 10;
int n, m;
int p[N], vis[N << 1], u[N << 1], v[N << 1];
int dsu(int x){
return p[x] == x ? x : p[x] = dsu(p[x]);
}
void init(){
memset(vis, 0, sizeof(vis));
memset(p, 0, sizeof(p));
memset(u, 0, sizeof(u));
memset(v, 0, sizeof(v));
}
void solve(){
init();
cin >> n >> m;
for(int i = 1; i <= n; i++) p[i] = i;
for(int i = 1; i <= m; i++) cin >> u[i] >> v[i];
int tot = m;
for(int i = m; i >= 1; i--){
int x = dsu(u[i]), y = dsu(v[i]);
if(x != y){
tot --;
vis[i] = 1;
p[x] = y;
}
}
cout << tot << '\n';
for(int i =1 ; i <= m; i++){
if(!vis[i]) cout << i << ' ';
}
cout << '\n';
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//cout << fixed << setprecision(9);
int t;
cin >> t;
while(t--){
solve();
}
}
1007.Shinobu loves trip
题意:
有P个城市,P为质数,对于编号为i的城市,第二天只能到达编号为(i * a)% P的城市。现在有n个旅游计划,包含起点和天数,有若干个询问,每次问路经一个指定城市的计划数。
题解:
从s走d天之后会来到(s * a) %P,因此,判断一个点x是否会被一个计划经过,只需要判断是否存在一个0 ≤ k ≤ d使得(s * ) % P = x,由于P是质数,所以已知x和s的值,可以得到的值。
预处理所有a的0次方到200000次方,然后直接查询算出来的是否在预处理出的数里面,如果不在,这个计划一定不经过x,否则可以得到k的具体值,判断k ≤ d即可。
要注意特判s = 0的情况。
代码:
#include<iostream>
#include<cmath>
#include<assert.h>
#include<iomanip>
#include<vector>
#include<cstring>
#include<unordered_map>
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int N = 2e5 + 10;
int p, a, n ,q;
int qm(int a, int b, const int p){
int res = 1;
while(b){
if(b % 2) res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
void solve(){
cin >> p >> a >> n >> q;
assert(p > 2 && p <= 1e9 + 7);
assert(a >= 2 && a < p);
assert(n <= 1000 && n > 0);
assert(q <= 1000 && q > 0);
unordered_map<int, int> f;
vector<int> s(n), is(n), d(n);
int cur = 1, len = 0;
while(len < N && !f.count(cur)){
f[cur] = len;
cur = cur * a % p;
len ++;
}
for(int i = 0; i < n; i ++){
cin >> s[i] >> d[i];
assert(s[i] < p && s[i] >= 0);
assert(d[i] <= 200000 && d[i] >= 1);
if(s[i]) is[i] = qm(s[i], p - 2, p);
}
while(q--){
int x;
cin >> x;
assert(x >= 0 && x < p);
int ans = 0;
if(x == 0) {
for(int i = 0; i < n; i++) if(s[i] == 0) ans ++;
}
else{
for(int i = 0; i < n; i++){
if(s[i] == 0) continue;
int ak = x * is[i] % p;
if(f.count(ak) && f[ak] <= d[i]) ans++;
}
}
cout << ans << '\n';
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//cout << fixed << setprecision(9);
int t;
cin >> t;
assert(t <= 5 && t > 0);
while(t--){
solve();
}
}