# Y

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 93    Accepted Submission(s): 32

Problem Description

Sample Input
4 1 2 1 3 1 4

Sample Output
1
Hint
1. The only set is {2,3,4}. 2. Please use #pragma comment(linker, "/STACK:16777216")

Source

Recommend
zhuyuanchen520

#pragma comment(linker, "/STACK:16777216")
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int V = 100000 + 50;
const int mod = 1000000000 + 7;
vector<int> vec[V];
int n;
bool vis[V];
__int64 ans;
int dfs(int index) {
int sum = 0;
vis[index] = true;
for(int i = 0; i < vec[index].size(); ++i)
if(!vis[vec[index][i]]) {
int temp = dfs(vec[index][i]);
sum += temp;
ans += (__int64) temp * (n - 1 - temp);
//printf("%d %d %I64d\n", temp, sum, ans);
}
if(sum)
ans += (__int64) sum * (n - 1 - sum);
sum++;
return sum;
}
int main() {
int i, j;
while(~scanf("%d", &n)) {
ans = 0;
memset(vis, false, sizeof(vis));
for(i = 1; i < n; ++i) {
int a, b;
scanf("%d%d", &a, &b);
vec[a].push_back(b);
vec[b].push_back(a);
}
dfs(1);
__int64 total = (__int64) n * (n - 1) * (n - 2) / 6;
printf("%I64d\n", total - ans / 2);
for(i = 1; i <= n; ++i)
vec[i].clear();
}
}

#pragma comment(linker, "/STACK:16777216")
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int V = 100000 + 50;
const int mod = 1000000000 + 7;
int pnt[V * 2], nxt[V * 2], e, head[V * 2];
int n;
bool vis[V];
__int64 ans;
inline void addedge(int u, int v) {
pnt[e] = v;
}
int dfs(int u) {
int sum = 0;
vis[u] = true;
for(int i = head[u]; i != -1; i = nxt[i])
if(!vis[pnt[i]]) {
int temp = dfs(pnt[i]);
sum += temp;
ans += (__int64) temp * (n - 1 - temp);
//printf("%d %d %I64d\n", temp, sum, ans);
}
if(sum)
ans += (__int64) sum * (n - 1 - sum);
sum++;
return sum;
}
int main() {
int i, j;
while(~scanf("%d", &n)) {
ans = e = 0;
for(i = 0; i <= n; ++i) {
vis[i] = false;
}
for(i = 1; i < n; ++i) {
int a, b;
scanf("%d%d", &a, &b);