Codeforces Round #177 (Div. 1)

Source: Internet
Author: User

D:

It is difficult to find the number of non-overlapping paths in a tree. It is much easier to reverse the process. c (n, 2) * c (n, 2) minus illegal

For each subtree, it can be divided into two types: intersection within the subtree and extension of external points to the inside of the subtree. Just cut them out.


[Cpp]
# Include <cstdio>
# Include <vector>
# Include <iostream>
Using namespace std;
Int a [80001];
Vector <int> edge [80001];
Long ans;
Int n;
Void dfs (int u, int f)
{
A [u] = 1;
Long in = 0;
For (int I = 0; I <edge [u]. size (); I ++)
{
Int v = edge [u] [I];
If (v = f) continue;
Dfs (v, u );
In + = (long) a [u] * a [v];
A [u] + = a [v];
}
Ans-= in * (in + 2 * (long) a [u] * (n-a [u]);
}
Int main ()
{
Int a, B, I;
Cin> n;
For (I = 1; I <n; I ++)
{
Cin> a> B;
Edge [a]. push_back (B );
Edge [B]. push_back ();
}
Ans = (long) n * (n-1)/2;
Ans * = ans;
Dfs (1, 0 );
Cout <ans <endl;
Return 0;
}

# Include <cstdio>
# Include <vector>
# Include <iostream>
Using namespace std;
Int a [80001];
Vector <int> edge [80001];
Long ans;
Int n;
Void dfs (int u, int f)
{
A [u] = 1;
Long in = 0;
For (int I = 0; I <edge [u]. size (); I ++)
{
Int v = edge [u] [I];
If (v = f) continue;
Dfs (v, u );
In + = (long) a [u] * a [v];
A [u] + = a [v];
}
Ans-= in * (in + 2 * (long) a [u] * (n-a [u]);
}
Int main ()
{
Int a, B, I;
Cin> n;
For (I = 1; I <n; I ++)
{
Cin> a> B;
Edge [a]. push_back (B );
Edge [B]. push_back ();
}
Ans = (long) n * (n-1)/2;
Ans * = ans;
Dfs (1, 0 );
Cout <ans <endl;
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.