#include<bits/stdc++.h> #define VAR(i,n) __typeof(n) i = (n) #define loop(i,j,s) for(int i=j;i<s;i++) #define loopback(i,j,s) for(int i=j;i>=s;i--) #define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) #define pln( x ) cout << x << "\n" #define ps( x ) cout << x << " " #define entr cout << "\n" #define pcnt(i) __builtin_popcount(i) #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define SIZE(c) (c).size() #define ALL(c) (c).begin(), (c).end() using namespace std; typedef vector<int> VI; typedef pair<int, int> PII; typedef vector<vector<int> > VVI; const int INFTY=20000000; const int MAX=300100; const int MOD=10000000; void coutTab(int* tab,int n){ loop(i,0,n){ cout<<tab[i]<<" "; } cout<<"\n"; } template<class T> void coutVec(vector<T> tab){ for(T t : tab){ cout<<t<<" "; } cout<<"\n"; } //------------------------------------------ VVI G(MAX); VVI GT(MAX); VVI SCC(MAX); VVI GC(MAX); ll a[MAX]; ll r[MAX]; int n; int order[MAX]; bool visited[MAX]; int rSCC[MAX]; void dfs1(int v){ visited[v] = 1; static int ord_i = 0; loop(i,0,G[v].size()) { if(!visited[G[v][i]]) { dfs1(G[v][i]); } } order[ord_i++] = v; } int cc_i = 0; void dfs2(int v){ visited[v] = 1; SCC[cc_i].pb(v); rSCC[v]=cc_i; loop(i,0,GT[v].size()) { if(!visited[GT[v][i]]) { dfs2(GT[v][i]); } } } void sss() { loop(i,0,n){ if(!visited[i]) dfs1(i); } reverse(order, order+n); loop(i,0,n) visited[i] = false; loop(i,0,n){ if(!visited[order[i]]){ dfs2(order[i]); cc_i++; } } loop(i,0,cc_i) { for(auto v : SCC[i]) { for(auto w: G[v]){ if(rSCC[w] != i) GC[i].pb(rSCC[w]); } } sort(ALL(GC[i])); GC[i].erase(unique(ALL(GC[i])), GC[i].end()); } } bool is_col(int i, int mask) { return (1<<i)&mask; } bool is_ok(int mask) { loop(i,0,cc_i) { if(is_col(i,mask)) { for(auto v : GC[i]) { if(!is_col(v,mask)) { return false; } } } } return true; } int main(){ ios_base::sync_with_stdio(0); cin>>n; loop(i,0,n) { cin>>a[i]>>r[i]; } loop(i,0,n) { loop(j,0,n) { if(i!=j && abs(a[i] - a[j])<=r[i]) { G[i].pb(j); GT[j].pb(i); } } } sss(); /*loop(i,0,cc_i) { ps(i);ps(":"); for(auto v: GC[i]) ps(v); entr; }*/ int res = 0; loop(mask,0,(1<<cc_i)) { if(is_ok(mask)) res++; } pln(res); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #include<bits/stdc++.h> #define VAR(i,n) __typeof(n) i = (n) #define loop(i,j,s) for(int i=j;i<s;i++) #define loopback(i,j,s) for(int i=j;i>=s;i--) #define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) #define pln( x ) cout << x << "\n" #define ps( x ) cout << x << " " #define entr cout << "\n" #define pcnt(i) __builtin_popcount(i) #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define SIZE(c) (c).size() #define ALL(c) (c).begin(), (c).end() using namespace std; typedef vector<int> VI; typedef pair<int, int> PII; typedef vector<vector<int> > VVI; const int INFTY=20000000; const int MAX=300100; const int MOD=10000000; void coutTab(int* tab,int n){ loop(i,0,n){ cout<<tab[i]<<" "; } cout<<"\n"; } template<class T> void coutVec(vector<T> tab){ for(T t : tab){ cout<<t<<" "; } cout<<"\n"; } //------------------------------------------ VVI G(MAX); VVI GT(MAX); VVI SCC(MAX); VVI GC(MAX); ll a[MAX]; ll r[MAX]; int n; int order[MAX]; bool visited[MAX]; int rSCC[MAX]; void dfs1(int v){ visited[v] = 1; static int ord_i = 0; loop(i,0,G[v].size()) { if(!visited[G[v][i]]) { dfs1(G[v][i]); } } order[ord_i++] = v; } int cc_i = 0; void dfs2(int v){ visited[v] = 1; SCC[cc_i].pb(v); rSCC[v]=cc_i; loop(i,0,GT[v].size()) { if(!visited[GT[v][i]]) { dfs2(GT[v][i]); } } } void sss() { loop(i,0,n){ if(!visited[i]) dfs1(i); } reverse(order, order+n); loop(i,0,n) visited[i] = false; loop(i,0,n){ if(!visited[order[i]]){ dfs2(order[i]); cc_i++; } } loop(i,0,cc_i) { for(auto v : SCC[i]) { for(auto w: G[v]){ if(rSCC[w] != i) GC[i].pb(rSCC[w]); } } sort(ALL(GC[i])); GC[i].erase(unique(ALL(GC[i])), GC[i].end()); } } bool is_col(int i, int mask) { return (1<<i)&mask; } bool is_ok(int mask) { loop(i,0,cc_i) { if(is_col(i,mask)) { for(auto v : GC[i]) { if(!is_col(v,mask)) { return false; } } } } return true; } int main(){ ios_base::sync_with_stdio(0); cin>>n; loop(i,0,n) { cin>>a[i]>>r[i]; } loop(i,0,n) { loop(j,0,n) { if(i!=j && abs(a[i] - a[j])<=r[i]) { G[i].pb(j); GT[j].pb(i); } } } sss(); /*loop(i,0,cc_i) { ps(i);ps(":"); for(auto v: GC[i]) ps(v); entr; }*/ int res = 0; loop(mask,0,(1<<cc_i)) { if(is_ok(mask)) res++; } pln(res); } |