Hdu-4619-Warm up 2

Source: Internet
Author: User

Question: The 1X2 bone card covers the plane. The input ensures that the horizontal bone cards (n) do not overlap, and the vertical bone cards (m) do not overlap, but the horizontal and vertical parts may overlap, ask how many dominoes do not interwork with each other after getting some dominoes (1 <= n, m <= 1000, the coordinate of the dominoes is (x, y) (0 <= x, y <= 100 )).

--> In the second multi-school case, WA5 hours is still WA, which is hard to understand...

Policy: Use and check the set. The two dominoes at the intersection are in the same set, but there cannot be loops. If a set has k elements, you can leave (k + 1)/2.

 

#include <cstdio>#include <cstring>#include <vector>using namespace std;const int maxn = 100 + 10;const int maxv = 2000 + 10;vector<int> G[maxn][maxn];int f[maxv], w[maxv];void init(){    int i, j;    for(i = 0; i < maxn; i++)        for(j = 0; j < maxn; j++)            G[i][j].clear();    for(i = 0; i < maxv; i++) f[i] = i;    for(i = 0; i < maxv; i++) w[i] = 1;}int Find(int x){    return x == f[x] ? x : Find(f[x]);}void Union(int x, int y){    int newx = Find(x);    int newy = Find(y);    if(newx != newy)    {        f[newy] = newx;        w[newx] += w[newy];    }}int main(){    int n, m, x, y, i, j;    while(scanf("%d%d", &n, &m) == 2)    {        if(!n && !m) return 0;        init();        for(i = 0; i < n; i++)        {            scanf("%d%d", &x, &y);            G[y][x].push_back(i);            G[y][x+1].push_back(i);        }        int N = n + m;        for(i = n; i < N; i++)        {            scanf("%d%d", &x, &y);            G[y][x].push_back(i);            G[y+1][x].push_back(i);        }        for(i = 0; i <= 100; i++)            for(j = 0; j <= 100; j++)                if(G[i][j].size() == 2) {                        Union(G[i][j][0], G[i][j][1]);}        int ret = 0;        for(i = 0; i < N; i++) if(f[i] == i) ret += (w[i] + 1) / 2;        printf("%d\n", ret);    }    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.