[Graph Theory 05] and query set 1006 zjnu Stadium

Source: Internet
Author: User

Algorithm concept: Query sets.

At the beginning, I didn't want to understand how to save the weight value, that is, distance dis. After reading the code of Daniel, I suddenly realized that the idea was still "path compression + Dynamic Weight Update" in the findset function ". The key is to find out where to update the weight (there are two parts in the code below to update the weight ).

Note: The distance between DIS and its parent node is saved at the beginning. After the findset is executed, the distance from DIS to the root node is changed to the node on the path. Also, the code "dis [a1] = (x + dis [B]-Dis [a] + 300) % 300 is compressed by the path strength; "There is no problem. You only need to update the DIS of A1.

There are two pieces of code below. The difference is that the input statement is changed. We can see that the time consumption saved by scanf is quite impressive !!!

515msac code:

// Template start # include <string> # include <vector> # include <algorithm> # include <iostream> # include <sstream> # include <fstream> # include <map> # include <set> # include <cstdio> # include <cmath> # include <cstdlib> # include <ctime> # include <iomanip> # include <string. h> # define SZ (x) (INT (X. size () using namespace STD; int toint (string s) {istringstream sin (s); int t; sin> T; return t ;} template <class T> string TOST Ring (t x) {ostringstream sout; sout <X; return sout. STR ();} typedef long int64; int64 toint64 (string s) {istringstream sin (s); int64 t; sin> T; return t ;} template <class T> T gcd (t a, t B) {if (a <0) return gcd (-a, B); If (B <0) return gcd (A,-B); Return (B = 0 )? A: gcd (B, A % B);} // template end (General Part) # define ifs CIN # define max_size 50005int next_node [max_size]; // storage edge // int flag [max_size]; // mark whether a node exists // int in [max_size]; // storage node's inbound degree // int out [max_size]; // storage node's outbound degree int dis [max_size]; // The node's Weight Value int m; void Init () // initialize {for (INT I = 0; I <m; I ++) {next_node [I] = I; DIS [I] = 0 ;} // memset (in, 0, sizeof (in); // memset (Out, 0, sizeof (out); // memset (flag, 0, sizeof (FLAG);} int findset (int A) // find the representative element of the set where the element is located (because path compression is used, the main purpose of path compression is to determine the set of elements as soon as possible.) {if (a = next_node [a]) {return next_node [a];} else {int T = next_node [a]; next_node [a] = findset (next_node [a]); // path compression (recursion) dis [a] = (DIS [a] + dis [T]) % 300; // update disreturn next_node [a] ;}} int union_nodes (int A, int B, int X) // set merge {int a1 = findset (a); int b1 = findset (B); If (a1 = b1) {If (DIS [B] + x) % 300 = dis [a]) {return 0;} else {return 1 ;}} else {next_node [a1] = b1; DIS [a1] = (x + dis [B]-Dis [a] + 300) % 300; return 0 ;}} // [Graph Theory 05] and query set 1006 zjnu stadiumint main () {// ifstream ifs ("shuju.txt", IOS: In); int N; int A, B, x; while (IFS> m> N) {Init (); int COUNT = 0; For (INT I = 0; I <n; I ++) {ifs> A> B> X; count + = union_nodes (a, B, x) ;}cout <count <Endl;} return 0 ;}

140msac code:

// Template start # include <string> # include <vector> # include <algorithm> # include <iostream> # include <sstream> # include <fstream> # include <map> # include <set> # include <cstdio> # include <cmath> # include <cstdlib> # include <ctime> # include <iomanip> # include <string. h> # define SZ (x) (INT (X. size () using namespace STD; int toint (string s) {istringstream sin (s); int t; sin> T; return t ;} template <class T> string TOST Ring (t x) {ostringstream sout; sout <X; return sout. STR ();} typedef long int64; int64 toint64 (string s) {istringstream sin (s); int64 t; sin> T; return t ;} template <class T> T gcd (t a, t B) {if (a <0) return gcd (-a, B); If (B <0) return gcd (A,-B); Return (B = 0 )? A: gcd (B, A % B);} // template end (General Part) # define ifs CIN # define max_size 50005int next_node [max_size]; // storage edge // int flag [max_size]; // mark whether a node exists // int in [max_size]; // storage node's inbound degree // int out [max_size]; // storage node's outbound degree int dis [max_size]; // The node's Weight Value int m; void Init () // initialize {for (INT I = 0; I <m; I ++) {next_node [I] = I; DIS [I] = 0 ;} // memset (in, 0, sizeof (in); // memset (Out, 0, sizeof (out); // memset (flag, 0, sizeof (FLAG);} int findset (int A) // find the representative element of the set where the element is located (because path compression is used, the main purpose of path compression is to determine the set of elements as soon as possible.) {if (a = next_node [a]) {return next_node [a];} else {int T = next_node [a]; next_node [a] = findset (next_node [a]); // path compression (recursion) dis [a] = (DIS [a] + dis [T]) % 300; // update disreturn next_node [a] ;}} int union_nodes (int A, int B, int X) // set merge {int a1 = findset (a); int b1 = findset (B); If (a1 = b1) {If (DIS [B] + x) % 300 = dis [a]) {return 0;} else {return 1 ;}} else {next_node [a1] = b1; DIS [a1] = (x + dis [B]-Dis [a] + 300) % 300; return 0 ;}} // [Graph Theory 05] and query set 1006 zjnu stadiumint main () {// ifstream ifs ("shuju.txt", IOS: In); int N; int A, B, x; while (IFS> m> N) {Init (); int COUNT = 0; For (INT I = 0; I <n; I ++) {scanf ("% d", & A, & B, & X); count + = union_nodes (a, B, x );} cout <count <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.