[Search] Niu Ke Practice Match 16 (similar to seeking Unicom block)

Link: https://www.nowcoder.com/acm/contest/84/C

Similar to the Unicom block, but I have never come into contact with this

It seems that the lack of systematic training is still very disadvantageous. Others have seen it.

And I foolishly used violence to write, I didn't expect to use search, and I didn't expect to use union search

 

Violent writing is stupid, only over 95

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define ll long long
 6 #define max3(a,b,c) fmax(a,fmax(b,c))
 7 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 8 
 9 bool cmp(const int &a,const int &b)
10 {
11     return a>b;
12 }
13 
14 int xx[1100],yy[1100];
15 
16 struct node
17 {
18     int x;
19     int y;
20 }d[1100];
21 
22 int main()
23 {
24     int n,p;
25     memset(xx,0,sizeof xx);
26     memset(yy,0,sizeof yy);
27     cin >> n;
28     p = 0;
29     for(int i = 1;i <= n;i++)
30     {
31         scanf("%d %d",&d[i].x,&d[i].y);
32         xx[d[i].x]++;
33         yy[d[i].y]++;
34     }
35     for(int i = 1;i <= n;i++)
36     {
37         if(xx[d[i].x] > 1 && yy[d[i].y] > 1)
38         {
39             p += 3;
40             xx[d[i].x] -= 2;
41             yy[d[i].y] -= 2;
42         }
43         else
44         {
45             if(xx[d[i].x] > 1 || yy[d[i].y] > 1)
46             {
47                 p += 1;
48                 if(xx[d[i].x] > 1)
49                     xx[d[i].x]--;
50                 else
51                     yy[d[i].y]--;
52             }
53         }
54     }
55     p = n-p-1;
56     // if(p > 0)
57         printf("%d\n",p);
58     // else
59     //     printf("0\n");
60     // if(p > 1)
61     //     printf("%d\n",p);
62     // else
63     //     printf("0\n");
64     return 0;
65 }
View Code

 

The search spelling now looks pretty neat

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define ll long long
 6 #define max3(a,b,c) fmax(a,fmax(b,c))
 7 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 8 
 9 int m[1010][1010];
10 void dfs(int x,int y)
11 {
12     m[x][y] = 0;
13     for(int i = 1;i <= 1000;i++)
14     {
15         if(m[x][i] == 1)    dfs(x,i);
16         if(m[i][y] == 1)    dfs(i,y);
17     }
18     return ;
19 }
20 
21 int main()
22 {
23     int n,x,y;
24     ll ans = 0;
25     cin >> n;
26     memset(m,0,sizeof m);
27     for(int i = 1;i <= n;i++)
28     {
29         scanf("%d %d",&x,&y);
30         m[x][y] = 1;
31     }
32     for(int i = 1;i <= 1000;i++)
33     {
34         for(int j = 1;j <= 1000;j++)
35         {
36             if(m[i][j] == 1)
37             {
38                 dfs(i,j);
39                 ans++;
40             }
41         }
42     }
43     printf("%lld\n",ans-1);
44     
45     return 0;
46 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325015610&siteId=291194637