题意:
就是给你n个点,n-1条边。然后每条边有一个权值,现在问你一共有多少漂亮的简单的路径,定义漂亮的简单路径为,每个点只能走一次,并且边的权值是要上升的。
思考:
- 当时推了推样例,看出来题意,然后怎么计数方案呢?题目说的看起来是个树,但是树上dp计数也不太好弄。然后于说把边看成点建图然后拓扑序求方案树,实际上这样不对。因为这样转移的时候,会走一条边走多次。
- 感觉就是当时,还看着别的题,这俩题都没思考出来。也没深入的去思考怎么怎么做。实际上就按边权排序就可以了,既然要让边权升序的走,那么每次枚举相同边权的边,那么这一层到a的方案数加上来,到b的方案数加上来。最后再把这一层的方案书加到总方案书ans里面,然后一步一步去转移就可以了
代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#define int long long
#define PII pair<int,int >
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int mod = 1e9+7,inf = 1e18;
const int N = 2e5+10,M = 2010;
int T,n,m,k;
int va[N];
map<int,int> ans,cnt;
map<int,vector<PII> > mp;
signed main()
{
IOS;
cin>>n;
for(int i=1;i<n;i++)
{
int a,b,c;
cin>>a>>b>>c;
mp[c].pb({a,b});
}
for(auto t:mp)
{
cnt.clear();
for(auto tt:t.se) //用了这些条边后的方案数
{
cnt[tt.fi] += ans[tt.se]+1; //加1就是以t.se为起点,走到t.fi的方案书
cnt[tt.se] += ans[tt.fi]+1;
}
for(auto tt:cnt) ans[tt.fi] += tt.se; //总方案书加起来
}
int sum = 0;
for(auto t:ans) sum += t.se;
cout<<sum;
return 0;
}
/*
当然也可以把求边的路径方案书,换成点的,也就是刚开始让cnt[i] = 1,最后求完答案的时候,每个点的方案书再减去1就可以了。这种操作之前做dp的时候也用过,因为你必然是要让某个点是有方案数的。
for(int i=1;i<=n;i++) ans[i] = 1;
for(auto t:mp)
{
cnt.clear();
for(auto tt:t.se)
{
cnt[tt.fi] += ans[tt.se];
cnt[tt.se] += ans[tt.fi];
}
for(auto tt:cnt) ans[tt.fi] += tt.se;
}
for(auto t:ans) sum += t.se-1;
cout<<sum;
*/
总结:
多多思考,深入思考,试一试。