标签:hoc 16px bool i++ line 继承 main oid mes
题目传送门:http://hihocoder.com/problemset/problem/1192
大意:给出一棵$N$个点的树,边权为$1$,要求给每个点构造$M$个权值$v_1...v_M$,使得对于任意$i,j$,都有$dis(i,j)=\sum\limits_{i=1}^M |v_i-v_j|$。$N \leq 100$,要求构造出的答案满足$M \leq 100,-100 \leq v \leq 100$
$N \leq M$是这道题最好的构造性质
构造如下:每一次继承父亲的权值,并且将它自己的编号对应的权值变为$1$即可。易知这种构造方案符合条件
1 #include<bits/stdc++.h>
2 using namespace std;
3
4 struct Edge{
5 int end , upEd;
6 }Ed[201];
7 int head[101] , N , cntEd , m[101][101];
8 bool vis[101];
9
10 inline void addEd(int a , int b){
11 Ed[++cntEd].end = b;
12 Ed[cntEd].upEd = head[a];
13 head[a] = cntEd;
14 }
15
16 void dfs(int now){
17 vis[now] = 1;
18 for(int i = head[now] ; i ; i = Ed[i].upEd)
19 if(!vis[Ed[i].end]){
20 for(int j = 0 ; j < N ; j++)
21 m[Ed[i].end][j] = m[now][j] + (j == Ed[i].end);
22 dfs(Ed[i].end);
23 }
24 }
25
26 int main(){
27 ios::sync_with_stdio(0);
28 cin.tie(0);
29 cout.tie(0);
30 cin >> N;
31 for(int i = 1 ; i < N ; i++){
32 int a , b;
33 cin >> a >> b;
34 addEd(a , b);
35 addEd(b , a);
36 }
37 dfs(0);
38 cout << N << endl;
39 for(int i = 0 ; i < N ; i++){
40 for(int j = 0 ; j < N ; j++)
41 cout << m[i][j] << ‘ ‘;
42 cout << endl;
43 }
44 return 0;
45 }
标签:hoc 16px bool i++ line 继承 main oid mes
原文地址:https://www.cnblogs.com/Itst/p/9757477.html