7.29总结
A
注意到总人数极小,直接枚举三个不同的人判断即可
注意有两种判断正确的可能
#include<bits/stdc++.h>
using namespace std;
int m , f;
bool g[10][10];
int main(){
cin >> m;
for( int i = 1 ; i <= m ; i++ ){
int a , b;
cin >> a >> b;
g[a][b] = g[b][a] = 1;
}
for( int i = 1 ; i <= 5 ; i++ ){
for( int j = i + 1 ; j <= 5 ; j++ ){
for( int k = j + 1 ; k <= 5 ; k++ ){
if( g[i][j] == g[j][k] && g[j][k] == g[i][k] ) f = 1;
}
}
}
if(f) cout << "WIN";
else cout << "FAIL";
return 0;
}
B
题目要求费用最少且不能建在仓库上
那就建在仓库旁边,显然要比建在没有仓库相邻的地方更好
但是枚举
取最小值为答案
#include<bits/stdc++.h>
using namespace std;
int n , vis[105] , ans = 1e9;
struct node{
int y , z;
};
vector<node> g[105];
void dfs(int x , int fa , int p){
for( auto i:g[x] ){
if( i.y == fa ) continue;
if( i.y == 1 ){
ans = min( ans , p + i.z );
continue;
}
dfs(i.y , x , i.z + p);
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cin >> n;
for( int i = 1 ; i <= n ; i++ ){
int a , b , c;
cin >> a >> b >> c;
g[a].push_back({b , 0});
g[b].push_back({a , c});
}
dfs(1 , 0 , 0);
cout << ans;
return 0;
}
F
电路排线已经确定,所以每个节点的实际功率是确定的
遍历一遍统计每个非叶子节点的实际功率
插排的最大功率可以随意调换,所以只要把最大功率大的分给实际功率大的就行
如果能保证每个节点实际功率都小于分配的排插的最大功率,说明没有任何违规用电
注意:
1、根节点是插座不能参与调换;
2、不开long long_____
3、根节点也有
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n , a[100005] , f[100005] , sum[100005] , s;
int h[100005] , c[100005] , cnt;
bool p[100005];
vector<int> g[100005];
void dfs(int x , int fa){
if( !g[x].size() ) sum[x] = a[x] , p[x] = 1 ,s += a[x];
for( auto i:g[x] ){
if( i == fa ) continue;
dfs(i , x);
sum[x] += sum[i];
}
}
signed main(){
cin >> n;
for( int i = 1 ; i <= n ; i++ ) cin >> f[i] >> a[i];
for( int i = 1 ; i <= n ; i++ ) {
g[f[i]].push_back(i);
}
dfs(0 , -1);
if(s > 2200){
cout << "NO\n";
return 0;
}
for( int i = 1 ; i <= n ; i++ ){
if(p[i]) continue;
cnt++;
h[cnt] = sum[i];
c[cnt] = a[i];
}
sort(h + 1 , h + cnt + 1);
sort(c + 1 , c + cnt + 1);
for( int i = 1 ; i <= cnt ; i++ ){
if( h[i] > c[i] ){
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
return 0;
}
G
这题和D.距离一样 差不太多
可以“整体减空白”先把总的路线数算出来得到
还是可以分类讨论
也就是从
方案数是
2、
3、普遍的情况:
1
/
2
/ \
x 3
/ \
4 y
会被蛰的路线有
方案数是
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n , x , y , sum[300005] , f[300005][21] , d[300005];
vector<int> g[300005];
void dfs(int x , int fa){
d[x] = d[fa] + 1;
sum[x] = 1;
f[x][0] = fa;
for( int i = 1 ; i <= 20 ; i++ ) f[x][i] = f[f[x][i - 1]][i - 1];
for( auto i : g[x] ){
if( i == fa ) continue;
dfs(i , x);
sum[x] += sum[i];
}
}
int lca(int x , int y){
if( d[x] < d[y] ) swap(x , y);
for( int i = 20 ; i >= 0 ; i-- ){
if( d[f[x][i]] >= d[y] ) x = f[x][i];
}
if( x == y ) return x;
for( int i = 20 ; i >= 0 ; i-- ){
if( f[x][i] != f[y][i] ){
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
}
int fp(int x , int y){
for( int i = 20 ; i >= 0 ; i-- ){
if( ( 1 << i ) <= y ){
x = f[x][i];
y -= ( 1 << i );
}
}
return x;
}
signed main(){
cin >> n >> x >> y;
for( int i = 1 ; i < n ; i++ ){
int a , b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1 , 0);
int ans = n * ( n - 1 ) , dis = abs(d[x] - d[y]) - 1;
if( lca(x , y) == x ) ans -= ( n - sum[fp(y , dis)] ) * sum[y];
else if( lca(x , y) == y ) ans -= sum[x] * ( n - sum[fp(x,dis)] );
else ans -= sum[x] * sum[y];
cout << ans;
return 0;
}
H
若某个顶点被多个骨牌半部指向,则这些半部的圆点数量必须完全相同
但这个”半部的原点数“是未知的,考虑到
对于每一种情况,因为每条边最多放一个骨牌,直接枚举每一条边,如果边的两个端点组合起来所代表的骨牌没有被使用过,就可以把它用上
最后统计最小值,时间复杂度
#include<bits/stdc++.h>
using namespace std;
int n , m , a[10] , ans;
struct node{
int a , b;
} l[50];
void dfs(int x){
if( x > n ){
int p[7][7] = {} , cnt = 0;
for( int i = 1 ; i <= m ; i++ ){
int r = a[l[i].a] , q = a[l[i].b];
if( r > q ) swap(r , q);
if( p[r][q] ) continue;
cnt++;
p[r][q] = 1;
}
ans = max( ans , cnt );
return;
}
for( int i = 1 ; i <= 6 ; i++ ){
a[x] = i;
dfs(x + 1);
a[x] = 0;
}
}
int main(){
cin >> n >> m;
for( int i = 1 ; i <= m ; i++ ){
cin >> l[i].a >> l[i].b;
}
dfs(1);
cout << ans;
return 0;
}
总结
1、F题在判断实际总功率时把所有的
2、不开long long_____;
3、C里