"Optimize AC" to establish contact

Source: Internet
Author: User

Establish contact

"Question description"

The new semester began, but the students in the holiday group changed the telephone, so students can only re-establish contact.

There are a total of n students in the class, they have established a total of M contact, the teacher wants to know in the students each time the establishment of a link, there are a total number of students can contact each other.

"Input Requirements"

The first line has two numbers n and M, indicating that there are n students and M operations.
After M line, each line two number A and B, respectively, the student A and B to establish a link between students.

"Output Requirements"

Output total M-line, an integer per line, indicating how many pairs of students can contact each other after the first operation.

"Input Instance"

5 61 22 34 51 32 44 5

  

"Output instance"

13441010

"Other Notes"

1<=n,m<=100000

"Analysis of Test questions"

Why is it called "Optimizing AC"? Very simple, because only in accordance with the optimized method of writing code to AC, otherwise the result is wrong, and check set "enemy" that the problem is to solve this method, but not optimization is the time overrun. Maintain the size of each and every check set. Let's take a look at the code specifically.

Code

#include <iostream>using namespace Std;long long x,y,f[100001],n,m,size[100001],ans;int find (int x) {if (f[x]=      =X) return f[x];  Return F[x]=find (F[x]);       } void Merge (int v,int u) {int t1,t2;       T1=find (v);       T2=find (U); if (T1!=T2) {f[t2]=t1;size[t1]+=size[t2];}//after merging and checking the set and checking the size of the set to increase the return;}    Inline long Long read () {long long x,f=1;    Char Ch=getchar (); for (;!    IsDigit (CH); Ch=getchar ()) if (ch== '-') f=-1;    for (x=ch-' 0 '; IsDigit (Ch=getchar ()); x=x*10+ch-' 0 '); return x*f;} inline void write (long long x) {if (x==0) {Putchar (' 0 '); return;}    if (x<0) Putchar ('-'), x=-x;    int Len=0,buf[15];while (x) buf[len++]=x%10,x/=10; for (int i=len-1;i>=0;i--) Putchar (buf[i]+ ' 0 '); return;}    int main () {N=read (), M=read ();     for (int i=1;i<=n;i++) f[i]=i,size[i]=1;        for (int i=1;i<=m;i++) {x=read (), Y=read (); if (find (x)!=find (y)) {ans+=size[find (x)]*size[find (y)];merge (x, y);}        Need to merge two and check sets, for future queries to prepare write (ans); printf ("\ n");//Don't forget to write in no carriage return!! }}

"Optimize AC" to establish a connection

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.