HDU 6035 colorful tree
来源:互联网 发布:组策略 禁止软件运行 编辑:程序博客网 时间:2024/05/29 17:03
题目链接
HDU 6035 colorful tree
分析
参考
首先,我们单独考虑每种颜色,对于颜色
代码
#include<bits/stdc++.h>using namespace std;#define pb push_back#define mkp make_pair#define fi first#define se second#define ll long long#define M 1000000007#define all(a) a.begin(), a.end()const int maxn = 200100;int n, ca, c[maxn], dfn[maxn], tot;ll sz[maxn], ans;vector<int> ed[maxn];stack<int> vec[maxn];void dfs(int t, int fa){ dfn[t] = ++tot; sz[t] = 1; for(auto v : ed[t]){ if(v == fa) continue; dfs(v, t); sz[t] += sz[v]; int tmp = sz[v]; while(!vec[c[t]].empty() && dfn[vec[c[t]].top()] > dfn[t]){ tmp -= sz[vec[c[t]].top()]; vec[c[t]].pop(); } ans -= (ll)tmp * (tmp - 1) / 2; } vec[c[t]].push(t);}int main(){ while(~scanf("%d", &n)){ for(int i = 1; i <= n; ++i) scanf("%d", c + i); for(int i = 1; i < n; ++i){ int u, v; scanf("%d%d", &u, &v); ed[u].pb(v); ed[v].pb(u); } tot = 0; ans = (ll)n * n * (n - 1) / 2; dfs(1, 0); for(int i = 1; i <= n; ++i){ ed[i].clear(); int tmp = n; while(!vec[i].empty()){ tmp -= sz[vec[i].top()]; vec[i].pop(); } ans -= (ll)tmp * (tmp - 1) / 2; } printf("Case #%d: %lld\n", ++ca, ans); } return 0;}
dfs剪掉的是每种颜色分块后第一个顶点到叶子顶点构成的树中的块,具体来说是用颜色栈
不过这份代码会超内存,因为颜色栈
AC code
g改进一下我们用
#include<bits/stdc++.h>#define pb push_back#define mp make_pair#define PI acos(-1)#define fi first#define se second#define INF 0x3f3f3f3f#define INF64 0x3f3f3f3f3f3f3f3f#define random(a,b) ((a)+rand()%((b)-(a)+1))#define ms(x,v) memset((x),(v),sizeof(x))using namespace std;const int MOD = 1e9+7;const double eps = 1e-8;typedef long long LL;typedef long double DB;typedef pair<int,int> PII;const int maxn = 2e5+10;const int MAX_V = 1e5+10;int c[maxn];std::vector<int> G[maxn];int sz[maxn];int sum[maxn];LL ans;void dfs(int u,int fa) { sz[u] = 1; LL pre = sum[c[u]]; LL add = 0; for(auto v : G[u]){ if(v == fa)continue; dfs(v,u); sz[u] += sz[v]; LL tmp = sz[v]; // while (!vec[c[u]].empty() && dfn[vec[c[u]].top()] > dfn[v]) { // tmp -= sz[vec[c[u]].top()]; // vec[c[u]].pop(); // } // ans -= tmp*(tmp-1)/2; tmp -= (sum[c[u]] - pre); ans -= tmp * (tmp - 1) /2; add += tmp; pre = sum[c[u]]; } sum[c[u]] += add+1;}int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n; int kase =0; c[0] = 0; while (cin>>n) { for(int i=1 ; i<=n ; ++i)cin>>c[i]; for(int i=1 ; i<n ; ++i){ int u,v;cin>>u>>v; G[u].pb(v);G[v].pb(u); } ans = (LL)n*n*(n-1)/2; ms(sum,0); dfs(1,0); for(int i=1 ; i<=n ; ++i){ LL tmp = n - sum[i]; ans -= tmp*(tmp-1) /2; } for(int i=1 ; i<=n ; ++i)G[i].clear(); std::cout << "Case #" << ++kase << ": "<< ans<< '\n'; } return 0;}
阅读全文
0 0
- hdu 6035 Colorful Tree
- HDU 6035 colorful tree
- [HDU]-6035 Colorful Tree
- hdu 6035 Colorful Tree
- hdu--6035--Colorful Tree
- hdu 6035 Colorful Tree
- HDU 6035 Colorful Tree
- hdu 6035 Colorful Tree
- HDU 6035 Colorful Tree
- HDU 6035 Colorful Tree
- hdu 6035(Colorful Tree)
- Hdu-6035 Colorful Tree(dfs)
- hdu 6035 Colorful Tree(dfs)
- hdu 6035 Colorful Tree(dfs)
- Colorful Tree(HDU 6035)
- HDU 6035 Colorful Tree dfs
- HDU 6035 Colorful Tree [树形dp]
- HDU 6035 Colorful Tree(虚树)
- ZIP数据压缩/解压工具
- 自定义可拖拽显示数字BadgeView(仿QQ可拖拽控件)
- Retrofit的简单使用
- iOS开发资源
- 【南阳OJ 91】 阶乘之和(贪心)
- HDU 6035 colorful tree
- ios开发之duplicate symbol for architecture x86_64 错误
- 1011. A+B和C (15)
- adb命令总结
- Java设计模式学习09——装饰模式
- vue与vue-i18n结合实现后台数据的多语言切换
- EasyUI学习总结(一)——EasyUI入门
- 应用MaxCompute实现变压器局部放电相位分析
- VertrigoServ设置多项目,不同端口打开