Building Forest CodeForces, forestcodeforces

Source: Internet
Author: User

Building Forest CodeForces, forestcodeforces

Building Forest CodeForces-195E

This is really hard to understand... It means "An oriented weighted forest is an acyclic weighted digraph in which from each vertex at most one edge goes." What does this sentence mean...

N vertices need to be inserted into a forest with edge weights in order of 1-n (the forest does not have any points or edges at the beginning ), line I + 1 describes the operation after point I is inserted. If a row is (K, Zookeeper,V1. zookeeper,X1. zookeeper,V2, Zookeeper,X2, please wait..., please wait ,...,VK, Zookeeper,XK), Then k indicates the number of edges to be connected. (vj, xj) indicates that an edge is to be connected to I from vertex vj, its weight is the total length of the path from vj to the root node of the original tree plus xj. (Ensure that data does not produce duplicate edges)

Practice: After understanding the question, you will find that this is a very normal (shui) weighted query set.

1 # include <cstdio> 2 # define md 1000000007 3 typedef long LL; 4 LL fa [100100]; 5 LL hei [100100]; // The total path length from record point I to Father's Day is 6 LL ans, n; 7 LL find (LL x) 8 {9 if (fa [x] = x) return x; 10 LL t = find (fa [x]); 11 hei [x] = (hei [fa [x] + hei [x]) % md; 12 fa [x] = t; 13 return fa [x]; 14} 15 int main () 16 {17 LL I, j, k, v, x, f1; 18 scanf ("% I64d", & n); 19 for (I = 1; I <= n; I ++) 20 fa [I] = I; 21 for (I = 1; I <= n; I ++) 22 {23 scanf ("% I64d", & k); 24 for (j = 1; j <= k; j ++) 25 {26 scanf ("% I64d % I64d", & v, & x); 27 f1 = find (v ); 28 // if (f1 = I) continue; 29 fa [f1] = I; 30 hei [f1] = (x + hei [v]) % md; 31 ans = (ans + hei [f1]) % md; 32} 33} 34 printf ("% I64d", (ans + md) % md); 35 return 0; 36}

Mistakes made:

1. The answer is not modulo, and a negative number is output (twice written, twice wrong)

2. Misunderstanding: 12 lines

1 # include <cstdio> 2 # define md 1000000007 3 typedef long LL; 4 LL fa [100100]; 5 LL hei2 [100100]; // The weight of the edge of the record point I connected to his father's day point is 6 LL hei [100100]; // The total length of the path from the record point I to the Father's Day point is 7 LL ans, n; 8 LL find (LL x) 9 {10 if (fa [x] = x) return x; 11 LL t = find (fa [x]); 12 hei [x] = (hei [fa [x] + hei2 [x]) % md; 13 fa [x] = t; 14 return fa [x]; 15} 16 int main () 17 {18 LL I, j, k, v, x, f1; 19 scanf ("% I64d", & n ); 20 for (I = 1; I <= n; I ++) 21 fa [I] = I; 22 for (I = 1; I <= n; I ++) 23 {24 scanf ("% I64d", & k); 25 for (j = 1; j <= k; j ++) 26 {27 scanf ("% I64d % I64d", & v, & x); 28 f1 = find (v); 29 if (f1 = I) continue; 30 fa [f1] = I; 31 hei2 [f1] = (x + hei [v]) % md; 32} 33} 34 for (I = 1; I <= n; I ++) 35 ans = (ans + hei2 [I] + md) % md; 36 printf ("% I64d", ans); 37 return 0; 38}

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.