-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathValid Path.cpp
136 lines (108 loc) · 3.55 KB
/
Valid Path.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Solution by Rahul Surana
***********************************************************
You are given a tree with N nodes numbered from 1 to N.
A set S of nodes is called valid if there exist two vertices u and v (possibly, u=v) such that every node in S lies on the simple path from u to v.
Count the number of valid sets modulo 109+7.
Two sets are different if one node is included in one set and not in the other.
If there are multiple pairs (u,v) making a set valid, that set is still only counted once.
Input
The first line contains an integer T, the number of test cases. Then the test cases follow.
Each test case contains N lines of input.
The first line contains a single integer N, the number of tree nodes.
Each of the next N−1 lines contains two space-separated integers u and v representing an edge between nodes u and v.
Output
For each test case, output in a single line the number of valid sets modulo 109+7.
***********************************************************
*/
#include <bits/stdc++.h>
#define ll long long
#define vl vector<ll>
#define vi vector<int>
#define pi pair<int,int>
#define pl pair<ll,ll>
#define all(a) a.begin(),a.end()
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FOR(i,a) for(int i = 0; i < a; i++)
#define trace(x) cerr<<#x<<" : "<<x<<endl;
#define trace2(x,y) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<endl;
#define trace3(x,y,z) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<" | "<<#z<<" : "<<z<<endl;
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define MOD 1000000007
using namespace std;
int size[100000];
vector<vector<int>> adj(100000);
int ans;
bool v[100000];
int determine_size(int s,int p){
int c = 0;
for(auto u : adj[s]) {
if(u !=p) c+= determine_size(u,s);
}
size[s] = c + 1;
return size[s];
}
void dfs(int s,int p,int pc){
v[s] = true;
pc++;
int c = 0;
for(auto x : adj[s]){
if(x != p) {
// cout << s+1 << " -> " << x+1 << " " << ans <<" \n";
if(!v[x]) { c+=pc; dfs(x,s,c); }
}
}
ans+=c;
// cout << c << "\n";
}
int sop(vector<int> x){
int c = 0;
for(int i = 0; i < x.size(); i++)
for(int j = i+1; j < x.size();j++) c+= (x[i]%MOD*x[j]%MOD)%MOD;
return 2*c;
}
void answer(int s){
vector<int> c;
if(v[s]) return;
v[s] = true;
for(auto x : adj[s]){
if(!v[x]) { c.pb(size[x]); }
}
// cout << "*********" << s <<" " << size[s] << "***********\n";
// FOR(i,c.size()) cout << c[i] <<" ";
// cout << "\n";
ans= (ans%MOD + size[s]%MOD)%MOD;
// cout << "s "<<ans << "\n";
ans = (ans %MOD + sop(c)%MOD) %MOD;
// cout << "sop "<<ans << "\n";
}
int main()
{
fast_io;
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
FOR(i,100000) { adj[i].clear(); v[i] = false; size[i] = 0; }
ans = 0;
for(int i = 0; i < n-1; i++) {
int u,v;
cin >> u >> v;
u--;
v--;
adj[u].pb(v);
adj[v].pb(u);
}
determine_size(0,0);
// FOR(i,n) cout << size[i] <<" ";
// cout << "\n";
FOR(i,n) answer(i);
// for(int i = 0; i < n; i++) { for(int j = i; j < n-i; j++) v[j] = false; cout << "******" << i <<"*****\n"; dfs(i,0,0); ans++; }
cout << ans << "\n";
}
}