CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » May 2010 Contest » Rendezvous

Rendezvous

Problem code: TR1

  • All Submissions

All submissions for this problem are available.

The problem statement has been updated

There are N cities in Byteland numbered 1 to N with city 1 as the capital. The road network of Byteland has a tree structure. All the roads are bi-directional. Alice and Bob are secret agents working for the Bytelandian Security Agency (BSA). Each secret agent is assigned a random city each month, where he/she must be stationed for the complete month. At the end of each month, they come to the capital to submit their reports to the head of BSA. They always take the shortest path from their city to the capital. If Alice is assigned city A and Bob is assigned city B then they meet at a city C which is common to both their routes to the capital and then travel together from C to the capital.

Alice and Bob wish to spend maximum time together in any trip. So for any pair of assigned cities (A,B) they meet at a city C such that C is the farthest city from the capital and appears in the shortest path from capital to A and capital to B. Since this happens each month they compute this for each pair of assigned cities (A,B) and store it in a matrix M, where M[A][B] = C, the city where they meet.

The importance of a city C(according to Alice and Bob), Im(C) is defined as the number of times C appears in their matrix M. Alice and Bob are interested in finding the importance of each city. Since this output can be large, output the sum S defined as S = (sum i=1 to N) i*Im(i) modulo 1000000007.

Input

First line of input contains an integer t (t<=25), the number of test cases. Each test case starts with an integer N (1<=N<=10000), the number of cities
The next N-1 lines contain two space separated integers u v (1<=u,v<=N) denoting a road from u to v.

Output

For each test case output the required sum S

Example

Input:
3
5
1 2
1 3
2 4
2 5
3
1 2
1 3
1

Output:
41
12
1

Explanation
For the first test case, the matrix M is:
1 1 1 1 1
1 2 1 2 2
1 1 3 1 1
1 2 1 4 2
1 2 1 2 5

and the corresponding importance array is: 15 7 1 1 1
so the required sum is 1*15 + 2*7 + 3*1 + 4*1 + 5*1 = 41

For the second test case, the matrix M is:
1 1 1
1 2 1
1 1 3
and so the Importance array is: 7 1 1
So the required sum is 1*7 + 2*1 + 3*1 = 12


For the third test case, there is only one city, so the Matrix M just has one entry 1, so S = 1



Author: akhilravidas
Date Added: 10-04-2010
Time Limit: 1 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

length of each road is same?

mcsharma1990 @ 1 May 2010 03:30 PM

length of each road is same?

I rezolve this problem and it

Wanda92 @ 1 May 2010 05:13 PM

I rezolve this problem and it works on my pc.But the problem is that when I submit it I get a SIGKILL error.

Can you tell me why? I found some explinations on the Internet but only for C++.I'm working in Pascal.

What causes a SIGKILL error?

@problem creater/Adimn   can

Shoonya @ 1 May 2010 05:29 PM

@problem creater/Adimn

 

can we assume tree to be binary ?

I didn't understand the input

hi_i_am_amit @ 1 May 2010 06:40 PM

I didn't understand the input properly. What does it mean by a road from s to t?

@ADMIN All the roads are

dabbcomputers @ 1 May 2010 11:02 PM

@ADMIN

All the roads are bi-directional.

WHAT IS MEANT BY THIS LINE....???

EITHER THIS MEAN WE CAN MOVE UP OR DOWN (BIDIRECTION) OR EVERY ROOT HAVE 2 NODES....?

It means that you can move up

lg5293 @ 2 May 2010 12:07 AM

It means that you can move up and down

City 1 is always the root of

ankul_iiita @ 2 May 2010 12:20 AM

City 1 is always the root of the tree?

Shall we assume the tree to

ankul_iiita @ 2 May 2010 01:38 AM

Shall we assume the tree to be a binary tree?

@Mohit, Ankul: You shall

triplem @ 2 May 2010 04:26 AM

@Mohit, Ankul: You shall assume what you are told in the problem statement. You shall not assume anything else.

@Admin. I am getting runtime

blackBird @ 2 May 2010 10:39 AM

@Admin.

I am getting runtime error(OTHER) in my code (C++) . I tried to find out the error using assert. But I did  *not*  get SIGABRT even when i placed assert(1==2) as the first line of my code !!. I also tried submittig a dummy code just to check if the amount of memory i allocated was within limits, and it worked fine. So, can you please tell me wheres the problem !!

I have noticed that it

triplem @ 2 May 2010 11:40 AM

I have noticed that it appears to be saying runtime error (OTHER) for all runtime errors now, I think that is a bug in the system. But no, you will not be told where the problem is in your code.

I'm getting NZEC error in

Wanda92 @ 2 May 2010 12:35 PM

I'm getting NZEC error in pascal.The problem works on my pc.Can you give me any idea of how to pass over this error?

Did you read the message I

triplem @ 2 May 2010 12:54 PM

Did you read the message I just posted? You will not get hints during a contest, please do not ask for them.

Finally got accepted :)

mcsharma1990 @ 2 May 2010 01:03 PM

Finally got accepted :)

Its so nice sometime. After

codegambler @ 2 May 2010 01:54 PM

Its so nice sometime. After so many WA and after so many modification one time came that you are so confident that before submitting the code you know that it will be ACCEPTED. :D

Does the input have some sort

shadow @ 2 May 2010 02:06 PM

Does the input have some sort of order?

No, the edges can be in any

triplem @ 2 May 2010 02:08 PM

No, the edges can be in any order in the input.

Thanx!!!! One more doubt does

shadow @ 2 May 2010 02:15 PM

Thanx!!!!

One more doubt does a pair (s t) implies that t is at lower order than s?

No, the problem statement

triplem @ 2 May 2010 02:16 PM

No, the problem statement doesn't impose any such restriction.

Is this the case that the

shadow @ 2 May 2010 04:20 PM

Is this the case that the cities with smaller no are either above or at the same level as of cities with large number?

Like I said, you cannot

triplem @ 2 May 2010 04:25 PM

Like I said, you cannot assume anything not stated in the problem itself. You are given a list of roads, in any order; there are no restrictions at all over how these are numbered or ordered.

if the run time is 1s then

dabbcomputers @ 2 May 2010 07:10 PM

if the run time is 1s then what should be the complexity of the source code.....???

comment removed by admin

dabbcomputers @ 2 May 2010 07:18 PM

comment removed by admin

can  dere be any cycle in

code_lover @ 2 May 2010 07:35 PM

can  dere be any cycle in graph ??

@Amritpal.. you should not

mcsharma1990 @ 2 May 2010 07:57 PM

@Amritpal.. you should not ask ANYTHING related to Solution, be it complexity or anything.

@Pankaj, read the problem statement carefully, you will find the answer.

@Amritpal, You must not

akhilravidas @ 2 May 2010 08:06 PM
@Amritpal, You must not discuss solution complexity when the contest is in progress. @Pankaj, You may assume any layout of cities which is possible according to the problem description.

First line of input contains

urbaddy @ 3 May 2010 03:17 PM

First line of input contains an integer t (t<=25), the number of test cases. Each test case starts with an integer N (1<=N<=10000), the number of cities
The next N-1 lines contain two space separated integers s t (1<=s,t<=N) denoting a road from s to t.

what does t stands for??? 

No of test cases or anything else??

Unable to understand the sentence "The next N-1 lines contain two space separated integers s t (1<=s,t<=N) denoting a road from s to t."

 

please please explain!!!!

 

 

t has been (wrongly) used in

triplem @ 3 May 2010 03:35 PM

t has been (wrongly) used in two different places, but the description is pretty clear. The first number is the number of test cases; after that, each pair of numbers are two cities that are joined by a road.

Hi Stephen Tx for such a

urbaddy @ 3 May 2010 04:41 PM

Hi Stephen

Tx for such a quick response.

Can you please explain a little more about matrix M is formed w.r.t given example

For the first test case, the matrix M is:
1 1 1 1 1
1 2 1 2 2
1 1 3 1 1
1 2 1 4 2
1 2 1 2 5

Tx in

@Badrinath, Stephen The input

akhilravidas @ 3 May 2010 10:00 PM

@Badrinath, Stephen The input description problem statement has been updated to remove the confusion :)

@Badrinath: the matrix M is clearly defined in the problem statement itself.

Time limit:

bonep @ 4 May 2010 05:41 AM

Time limit: s

Languages:

(nothing?)

^--- something wrong with the description? (i think it happens after the last update)

/sources/Main.java:7: class

whirlpool @ 5 May 2010 03:06 PM

/sources/Main.java:7: class Rendezvous is public, should be declared in a file named Rendezvous.java public class Rendezvous { ^ Note: /sources/Main.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error

I am uploading a file Rendezvous.java and have compiled the code with -Xlint option. There are no warnings or error. Any pointers to uploading solution?

Admins???

whirlpool @ 5 May 2010 03:31 PM

Admins???

Have you considered, say,

triplem @ 5 May 2010 04:56 PM

Have you considered, say, reading something like the FAQ?

@ Admin-If i submit a java

calc_saransh @ 6 May 2010 02:08 PM

@ Admin-If i submit a java solution is the Main class only compiled??? or the classes I have in my code also get compiled???... i m getting wrong answer when I submit ... i think my solution is correct it may be a case of TLE but i do not thing it is wrong ... :)

If your code wasn't compiling

triplem @ 6 May 2010 02:21 PM

If your code wasn't compiling properly you would get a compile error. Wrong answer means your program is outputting the wrong results.

what is the limit of memory a

chetan1507 @ 6 May 2010 10:30 PM

what is the limit of memory a program can use??

admin??

chetan1507 @ 6 May 2010 11:11 PM

admin??

In case two person solve same

sushilnath @ 7 May 2010 12:13 AM

In case two person solve same number of prblems how the tie is broken

In case two person solve same

sushilnath @ 7 May 2010 12:13 AM

In case two person solve same number of prblems how the tie is broken

@sushil: ties aren't broken.

suh_ash2008 @ 7 May 2010 10:23 AM

@sushil: ties aren't broken. In case of prize money, it is shared.

Suppose the rank list says:

1 A

2 B

3 C

3 D

3 E

6 F

then:

A gets cash prize for 1st

B gets cash prize for 2nd

each of C, D, E get cash = (cash prize for 3rd + cash prize for 4th + cash prize for 5th) / 3;

F gets cash prize for 6th

S must be output modulo

Ra16bit @ 7 May 2010 05:29 PM

S must be output modulo 1000000007, or every member of this sum (i*Im(i) ) must be counted modulo 1000000007, but sum can exceed 1000000007?

@Yury You are expected to

akhil_adm @ 7 May 2010 09:07 PM

@Yury You are expected to compute S = ( (sum i=1 to N) i*Im(i) ) modulo 1000000007

@Akhil, thanks!

Ra16bit @ 7 May 2010 11:48 PM

@Akhil, thanks!

admin getting "internal error

chang @ 9 May 2010 09:42 PM

admin

getting "internal error in system" on submission.

me too getting "internal

srivatsan_vn @ 10 May 2010 01:49 AM

me too getting "internal error in system:(" upon submission

it works now, I saw my

vdmedragon @ 10 May 2010 02:19 AM

it works now, I saw my submission which was about 1h ago accepted. But the ranks list has not updated.

admin I am getting "an

ankit1990 @ 10 May 2010 07:21 AM

admin

I am getting "an internal error occurred in the system". Could you please clarify the meaning ?

i think the site is not

dabbcomputers @ 10 May 2010 09:49 AM

i think the site is not working properly everybody is getting the same error

yeah .. that too a day before

ankit1990 @ 10 May 2010 11:40 AM

yeah .. that too a day before the end of the competition :( !

can a test case have given

dabbcomputers @ 11 May 2010 08:41 PM

can a test case have given  disorder relation like lower level node relations enter first.

@amritpal... there is nothing

calc_saransh @ 11 May 2010 10:37 PM

@amritpal... there is nothing as such mentioned in the problem so the question does nt arise..

@saransh there is no any

dabbcomputers @ 12 May 2010 11:23 AM

@saransh

there is no any restriction in question about this type of test case, then they may have a like this....

i had the same approach as

shivmitra @ 14 May 2010 09:27 PM

i had the same approach as stated in the editorial.....bt i kept on getting WA.....can anyone provide the input cases file they used to chck their code..or can admin provide the test cases used for this problem.....

It is really easy to come up

triplem @ 15 May 2010 02:56 AM

It is really easy to come up with input cases and check them by hand for this problem, you don't need other people to do that for you.

for small cases i m getting

shivmitra @ 15 May 2010 10:34 PM
for small cases i m getting correct answer...i jst wanted to hav some bigger cases and correct answers for them...

You might want to generate a

f03nix @ 16 May 2010 12:38 AM

You might want to generate a simple case of 10000 cities with roads from u to v like "1 2 , 2 3 , 3 4 , ... , 9999 10000 etc". Or the one where the network of roads is like a maximally packed (or whatever you call it) binary tree. Then a min level tree of degree 3 , etc .

 

Not that this would cover all the cases (it can ?? ) ... but they should check most overflow conditions you might have :-?

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com