Bzoj1202 -- with permission and query set + prefix and, bzoj1202 -- prefix

Source: Internet
Author: User

Bzoj1202 -- with permission and query set + prefix and, bzoj1202 -- prefix

Http://www.lydsy.com/JudgeOnline/problem.php? Id = 1202

Note that s [I] = a [1] + a [2] +... + a [I], that is, s [I] is the prefix and. Then v [I] = s [f [I]-s [I], where f [I] is the father of I. For each read x, y, k, x and y are considered as nodes. If x and y are the same root nodes, because v [y]-v [x] = s [f [y]-s [y]-s [f [x]-s [x] and f [y] = f [x], therefore, v [y]-v [x] is the sum of the range [x, y]. Therefore, you only need to determine whether v [y]-v [x] is k. If the root nodes of x and y are different, merge the two nodes and update the information.

# Include <cstdio> # include <cstring> using namespace std; int n, I, j, m, f [101], w, x, y, k, v [101]; bool flag; int find (int x) {if (x = f [x]) return x; int tmp = f [x]; // record the original father f [x] = find (f [x]); v [x] + = v [tmp]; // update the node information return f [x];} bool union1 (int x, int y, int k) {int fx = find (x), fy = find (y ); if (fx = fy) {if (v [y]-v [x]! = K) return 0;} else {f [fy] = fx; v [fy] = k-v [y] + v [x]; // update node information} return 1;} int main () {scanf ("% d", & w); while (w --) {memset (v, 0, sizeof (v); flag = 0; scanf ("% d", & n, & m); for (I = 0; I <= n; I ++) f [I] = I; for (I = 1; I <= m; I ++) {scanf ("% d ", & x, & y, & k); if (! Flag &&! Union1 (x-1, y, k) flag = 1;} if (flag) printf ("false \ n"); else printf ("true \ n ");} 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.