5月6日小测深刻总结

· · 个人记录

5月6日小测深刻总结

C [语言月赛202211] Power Strip

思路

利用输入的数据生成一颗无环树,并把每个数的点权(充电器的数量)标在树上,利用递归逻辑,把每一个节点供电的充电器数量一个一个推上去,并记录,然后输出。

代码

1:

用dfs来搜索这棵树其最坏时间复杂度是O(2n-1)(邻接表),有几个测试点会 TLE,代码如下:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int n, ans;
int a[N];
bool vis[N];
vector<int> e[N];
void dfs(int u);
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 2; i <= n; i++){
        int x;
        cin >> x;
        e[x].push_back(i);
    }
    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++){
        sort (e[i].begin(), e[i].end());
    }
    for (int i = 1; i <= n; i++){
        memset (vis, false, sizeof (vis));
        ans = a[i];
        dfs(i);
        cout << ans << " ";
    }
    return 0;
}
void dfs(int u){
    int len = e[u].size();
    for (int i = 0; i < len; i++){
        if (vis[e[u][i]] == true){
            continue;
        }
        ans += a[e[u][i]];
        dfs(e[u][i]);
    }
    return ;
}

2

改为使用递归思想来解决,时间复杂度为O(n)代码如下:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int n;
int a[N], answer[N];
bool vis[N];
vector<int> e[N];
void dfs(int u);
signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 2; i <= n; i++){
        int x;
        cin >> x;
        e[x].push_back(i);
    }
    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++){
        sort (e[i].begin(), e[i].end());
    }
    memset (vis, false, sizeof (vis));
    dfs(1);
    for (int i = 1; i <= n; i++){
        cout << answer[i] << " ";
    }
    return 0;
}
void dfs(int u){
    int len = e[u].size();
    for (int i = 0; i < len; i++){
        if (vis[e[u][i]] == true){
            continue;
        }
        dfs(e[u][i]);
        answer[u] += answer[e[u][i]];
    }
    answer[u] += a[u];      
    return ;
}

错误的地方

### Ac代码 ```c++ #include<bits/stdc++.h> #define int long long using namespace std; const int N = 1e6 + 5; int n; int a[N], answer[N]; bool vis[N]; vector<int> e[N]; void dfs(int u); signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for (int i = 2; i <= n; i++){ int x; cin >> x; e[x].push_back(i); } for (int i = 1; i <= n; i++){ cin >> a[i]; } for (int i = 1; i <= n; i++){ sort (e[i].begin(), e[i].end()); } memset (vis, false, sizeof (vis)); dfs(1); for (int i = 1; i <= n; i++){ cout << answer[i] << " "; } return 0; } void dfs(int u){ int len = e[u].size(); for (int i = 0; i < len; i++){ if (vis[e[u][i]] == true){ continue; } dfs(e[u][i]); answer[u] += answer[e[u][i]]; } answer[u] += a[u]; return ; } ``` ## [D](https://www.luogu.com.cn/problem/P1195) 口袋的天空 ### 思路 因为每次连接两朵云,都相当于把两个连通块拼在一起,也就是问,要至少多少的代价,才能只剩k个连通块呢? ### 错误代码 ```c++ #include<bits/stdc++.h> #define int long long using namespace std; const int N = 1e4 + 5; int n, m, k; int a[N]; signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> m >> k; if (n - m - 1 > k || n < k){ cout << "No Answer" << "\n"; return 0; } cout << 1 << "\n"; return 0; } ``` ### Ac代码 ```c++ #include<bits/stdc++.h> #define int long long using namespace std; const int N = 2e5 + 5; int n, m, k, ans; int fa[N]; struct node{ int u, v, w; }a[N]; bool cmp(node q, node h){ return q.w < h.w; } int getfa(int x){ if (fa[x] == x){ return x; } return fa[x] = getfa(fa[x]); } signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> m >> k; for (int i = 1; i <= m; i++){ cin >> a[i].u >> a[i].v >> a[i].w; } sort (a + 1, a + m + 1, cmp); for (int i = 0; i <= n; i++){ fa[i] = i; } int num = 0, sum = 0; for (int i = 1; i <= m; i++){ int t1 = getfa(a[i].u); int t2 = getfa(a[i].v); if (t1 != t2){ num++; fa[t2] = t1; sum += a[i].w; } if (num == n - k){ cout << sum << "\n"; return 0; } } cout << "No Answer" << "\n"; return 0; } ```