graphs *2300

Please click on ads to support us..

C++ Code:

#include <stdlib.h>
#include <time.h>
#include <vector>
#include <math.h>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <map>
#include <stack>
#include <queue>
#include <utility>
#include <set>
#include <deque>
using namespace std;
typedef long long int ll;
const int M=200000+10;
 
ll mod = 1e9 + 7;
int x[M],y[M];
set<int> sx,sy;
map<int,int> mpx,mpy;

int pa[M];
int pan[M];
int pae[M];
int fpa(int x){
    return pa[x] = ( x == pa[x] ? x : fpa(pa[x]) );
}

ll two[M];
set<int> ss;
 int main() {
	
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	
  two[0] = 1;
   for(int i = 1; i < M; i++) two[i] = (two[i - 1] * 2) % mod;
	int n;
	cin>>n;
	for (int i = 1; i <= n; i++){
		cin>>x[i]>>y[i];
     sx.insert(x[i]); sy.insert(y[i]);
	}
   int cntx = 1;
	for(set<int>::iterator it = sx.begin(); it != sx.end(); it++){
       int v = (*it);
       mpx[v] = cntx++;
  }
  cntx--;
  int cnty = 1;
  for(set<int>::iterator it = sy.begin(); it != sy.end(); it++){
       int v = (*it);
       mpy[v] = cnty++;
  }
  cnty--;

  for(int i = 1; i < M; i++){
      pa[i] = i; pan[i] = 1;
  }
	for(int i = 1; i <= n; i++){
       int mx = mpx[x[i]]; int my = mpy[y[i]];
       int u = mx; int v = my + cntx;
       int pu = fpa(u); int pv = fpa(v);
       if(pu == pv){
           pae[pu]++;
       } else {
            pan[pu] = pan[pv] + pan[pu];
            pae[pu] = pae[pv] + pae[pu] + 1;
            pa[pv] = pu;
        }
  }
for(int i = 1; i <= cntx + cnty; i++){
       ss.insert(fpa(i));
  }
   ll res = 1;
  for(set<int>::iterator it = ss.begin(); it != ss.end(); it++){
       int v = (*it); ll tot;
       if(pan[v] == pae[v] + 1){
             tot = two[pan[v]] - 1;
       } else {
             tot = two[pan[v]];
        }
        res = (res * tot) % mod;
        if(res < 0) res += mod;
  }
   cout<<res<<endl;
	
	return 0;
}


Comments

Submit
0 Comments
More Questions

337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation