/***********Template Starts Here***********/ #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map> #include <queue> #include <stack> #include <vector> #include <deque> #include <functional> #include <string> #include <iostream> #include <cctype> #include <set> #include <climits> #include <iomanip> #include <cassert> //#include <unordered_map> #define pb push_back #define nl puts ("") #define sp printf ( " " ) #define phl printf ( "hello\n" ) #define ff first #define ss second #define POPCOUNT __builtin_popcountll #define RIGHTMOST __builtin_ctzll #define LEFTMOST(x) (63-__builtin_clzll((x))) #define MP make_pair #define FOR(i,x,y) for(int i = (x) ; i <= (y) ; ++i) #define ROF(i,x,y) for(int i = (y) ; i >= (x) ; --i) #define CLR(x,y) memset(x,y,sizeof(x)) #define UNIQUE(V) (V).erase(unique((V).begin(),(V).end()),(V).end()) #define MIN(a,b) ((a)<(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b)) #define NUMDIGIT(x,y) (((int)(log10((x))/log10((y))))+1) #define SQ(x) ((x)*(x)) #define ABS(x) ((x)<0?-(x):(x)) #define FABS(x) ((x)+eps<0?-(x):(x)) #define ALL(x) (x).begin(),(x).end() #define LCM(x,y) (((x)/gcd((x),(y)))*(y)) #define SZ(x) ((int)(x).size()) #define NORM(x) if(x>=mod)x-=mod; using namespace std; typedef long long vlong; typedef unsigned long long uvlong; typedef pair < int, int > pii; typedef pair < vlong, vlong > pll; typedef vector<pii> vii; typedef vector<int> vi; const vlong inf = 2147383647; const double pi = 2 * acos ( 0.0 ); const double eps = 1e-9; #ifdef forthright48 #include <ctime> clock_t tStart = clock(); #define debug(args...) {dbg,args; cerr<<endl;} #define timeStamp printf("Execution Time: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC) #else #define debug(args...) // Just strip off all debug tokens #define timeStamp #endif struct debugger{ template<typename T> debugger& operator , (const T& v){ cerr<<v<<" "; return *this; } }dbg; //int knightDir[8][2] = { {-2,1},{-1,2},{1,2},{2,1},{2,-1},{-1,-2},{1,-2},{-2,-1} }; //int dir4[4][2] = {{-1,0},{0,1},{1,0},{0,-1}}; inline vlong gcd ( vlong a, vlong b ) { a = ABS ( a ); b = ABS ( b ); while ( b ) { a = a % b; swap ( a, b ); } return a; } vlong ext_gcd ( vlong A, vlong B, vlong *X, vlong *Y ){ vlong x2, y2, x1, y1, x, y, r2, r1, q, r; x2 = 1; y2 = 0; x1 = 0; y1 = 1; for (r2 = A, r1 = B; r1 != 0; r2 = r1, r1 = r, x2 = x1, y2 = y1, x1 = x, y1 = y ) { q = r2 / r1; r = r2 % r1; x = x2 - (q * x1); y = y2 - (q * y1); } *X = x2; *Y = y2; return r2; } inline vlong modInv ( vlong a, vlong m ) { vlong x, y; ext_gcd( a, m, &x, &y ); if ( x < 0 ) x += m; //modInv is never negative return x; } inline vlong power ( vlong a, vlong p ) { vlong res = 1, x = a; while ( p ) { if ( p & 1 ) res = ( res * x ); x = ( x * x ); p >>= 1; } return res; } inline vlong bigmod ( vlong a, vlong p, vlong m ) { vlong res = 1 % m, x = a % m; while ( p ) { if ( p & 1 ) res = ( res * x ) % m; x = ( x * x ) % m; p >>= 1; } return res; } /***********Template Ends Here***********/ /*********** Hasib Templates Starts Here**********/ #include<bits/stdc++.h> using namespace std; #define loop(i, n) for(int i=0; i<(n); i++) #define sf scanf #define pf printf #define fr first #define sc second #define ll long long #define dd double #define all(v) (v).begin(), (v).end() #define PI acos(-1.0) #define mem(ara, val) memset(ara, val, sizeof(ara)) #define paii pair<int, int> #define pall pair<ll, ll> #define read(nm) freopen(nm, "r", stdin) #define write(nm) freopen(nm, "w", stdout) #define vdump(x) cerr<<#x<<" = "<<x<<endl; #define dump(args...) cerr,args; cerr<<endl template<typename T> ostream& operator<<(ostream& out, vector<T> v) { out<<"[ "; loop(i, SZ(v)) { if(i) out<<", "; out<<v[i]; } out<<" ]"; return out; } template<typename T1, typename T2> ostream& operator<<(ostream &out, pair<T1, T2> p) { out<<"( "<<p.fr<<", "<<p.sc<<")"; return out; } template<typename T> ostream& operator,(ostream &out, T x) { out<<x<<" "; return out; } /**********Hasib Templates Ends Here**************/ /** * WARTNING for me: * Never use FOR * Never use pii, pll, vi, vi * Never use ff, ss, phl, sp, nl */ #define NODE 5100 #define MAXX 107 struct KUHN{ int left[NODE], right[NODE], vis[NODE], cc; vector<int> adj[NODE]; KUHN() : cc(1) { } void clear(int n) { FOR(i, 0, n) adj[i].clear(); } bool tryK(int v) { if(vis[v] == cc) return false; vis[v] = cc; loop(i, SZ(adj[v])) { int t = adj[v][i]; if(right[t] == -1) { right[t] = v; left[v] = t; return true; } } loop(i, SZ(adj[v])) { int t = adj[v][i]; if( tryK(right[t])) { right[t] = v; left[v] = t; return true; } } return false; } int match(int n) { int res = 0; bool done; CLR(left, -1); CLR(right, -1); do{ done = true; cc++; FOR(i, 0, n) { if(left[i] == -1 && tryK(i)) { done = false; } } }while(!done); FOR(i, 0, n) res += (left[i] != -1); return res; } }kuhn; char grid[MAXX][MAXX]; int N, M; #define index sflajfsa paii index[MAXX][MAXX]; void solve() { int r = 0, c = 0; loop(i, N) { loop(j, M) { if(j == 0) { r++; if(grid[i][j] == '.') { index[i][j].fr = r; } } else { if(grid[i][j] == '.') { index[i][j].fr = r; } else { index[i][j].fr = -1; if(grid[i][j] == 'W' && grid[i][j-1] != 'W' ) { r++; } } } } } loop(j, M) { loop(i, N) { if(i == 0) { c++; if(grid[i][j] == '.') { index[i][j].sc = c; } } else { if(grid[i][j] == '.') { index[i][j].sc = c; } else { index[i][j].sc = -1; if(grid[i-1][j] != 'W' && grid[i][j] == 'W') { c++; } } } } } kuhn.clear(r+2); loop(i, N) { loop(j, M) { if(grid[i][j] == '.') { kuhn.adj[ index[i][j].fr ].pb( index[i][j].sc ); } } } int m = kuhn.match(r + 2); pf("%d\n", m); int u, v; loop(i, N) { loop(j, M) { if(grid[i][j] == '.') { u = index[i][j].fr; v = index[i][j].sc; if(kuhn.left[u] == v) { pf("T"); } else { pf("."); } } else { pf("%c", grid[i][j]); } } pf("\n"); } } int main() { #ifdef hasibpc read("input.txt"); //write("output.txt"); #endif int kases, kaseno = 0; sf("%d", &kases); while(kases--) { sf("%d %d", &N, &M); loop(i, N) { sf("%s", grid[i]); } pf("Case %d: ", ++kaseno); solve(); } }
Category Archives: Graph
(LightOJ) 1218 – Multiple Free Subset
0Step 1. Sort the array.
Step 2. Remove duplicates.
Step 3. Make a bipartite graph. There should be a connection from i to j iff i divides j.
Step 4. Start from the smallest number, and follow the next steps repeatedly.
– Remove all the multiples of this number from the graph. Remove from both sides of bipartite graph.
– Now check, if we can create exactly same size of maximum independent set as before.
– If possible, parmanently delete those multiples and push the current number into result array. If not possible, restore the previous graph.
/***********Template Starts Here***********/ #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map> #include <queue> #include <stack> #include <vector> #include <deque> #include <functional> #include <string> #include <iostream> #include <cctype> #include <set> #include <climits> #include <iomanip> #include <cassert> //#include <unordered_map> #define pb push_back #define nl puts ("") #define sp printf ( " " ) #define phl printf ( "hello\n" ) #define ff first #define ss second #define POPCOUNT __builtin_popcountll #define RIGHTMOST __builtin_ctzll #define LEFTMOST(x) (63-__builtin_clzll((x))) #define MP make_pair #define FOR(i,x,y) for(int i = (x) ; i <= (y) ; ++i) #define ROF(i,x,y) for(int i = (y) ; i >= (x) ; --i) #define CLR(x,y) memset(x,y,sizeof(x)) #define UNIQUE(V) (V).erase(unique((V).begin(),(V).end()),(V).end()) #define MIN(a,b) ((a)<(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b)) #define NUMDIGIT(x,y) (((int)(log10((x))/log10((y))))+1) #define SQ(x) ((x)*(x)) #define ABS(x) ((x)<0?-(x):(x)) #define FABS(x) ((x)+eps<0?-(x):(x)) #define ALL(x) (x).begin(),(x).end() #define LCM(x,y) (((x)/gcd((x),(y)))*(y)) #define SZ(x) ((int)(x).size()) #define NORM(x) if(x>=mod)x-=mod; using namespace std; typedef long long vlong; typedef unsigned long long uvlong; typedef pair < int, int > pii; typedef pair < vlong, vlong > pll; typedef vector<pii> vii; typedef vector<int> vi; const vlong inf = 2147383647; const double pi = 2 * acos ( 0.0 ); const double eps = 1e-9; #ifdef forthright48 #include <ctime> clock_t tStart = clock(); #define debug(args...) {dbg,args; cerr<<endl;} #define timeStamp printf("Execution Time: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC) #else #define debug(args...) // Just strip off all debug tokens #define timeStamp #endif struct debugger{ template<typename T> debugger& operator , (const T& v){ cerr<<v<<" "; return *this; } }dbg; //int knightDir[8][2] = { {-2,1},{-1,2},{1,2},{2,1},{2,-1},{-1,-2},{1,-2},{-2,-1} }; //int dir4[4][2] = {{-1,0},{0,1},{1,0},{0,-1}}; inline vlong gcd ( vlong a, vlong b ) { a = ABS ( a ); b = ABS ( b ); while ( b ) { a = a % b; swap ( a, b ); } return a; } vlong ext_gcd ( vlong A, vlong B, vlong *X, vlong *Y ){ vlong x2, y2, x1, y1, x, y, r2, r1, q, r; x2 = 1; y2 = 0; x1 = 0; y1 = 1; for (r2 = A, r1 = B; r1 != 0; r2 = r1, r1 = r, x2 = x1, y2 = y1, x1 = x, y1 = y ) { q = r2 / r1; r = r2 % r1; x = x2 - (q * x1); y = y2 - (q * y1); } *X = x2; *Y = y2; return r2; } inline vlong modInv ( vlong a, vlong m ) { vlong x, y; ext_gcd( a, m, &x, &y ); if ( x < 0 ) x += m; //modInv is never negative return x; } inline vlong power ( vlong a, vlong p ) { vlong res = 1, x = a; while ( p ) { if ( p & 1 ) res = ( res * x ); x = ( x * x ); p >>= 1; } return res; } inline vlong bigmod ( vlong a, vlong p, vlong m ) { vlong res = 1 % m, x = a % m; while ( p ) { if ( p & 1 ) res = ( res * x ) % m; x = ( x * x ) % m; p >>= 1; } return res; } /***********Template Ends Here***********/ /*********** Hasib Templates Starts Here**********/ #include<bits/stdc++.h> using namespace std; #define loop(i, n) for(int i=0; i<(n); i++) #define sf scanf #define pf printf #define fr first #define sc second #define ll long long #define dd double #define all(v) (v).begin(), (v).end() #define PI acos(-1.0) #define mem(ara, val) memset(ara, val, sizeof(ara)) #define paii pair<int, int> #define pall pair<ll, ll> #define read(nm) freopen(nm, "r", stdin) #define write(nm) freopen(nm, "w", stdout) #define vdump(x) cerr<<#x<<" = "<<x<<endl; #define dump(args...) cerr,args; cerr<<endl template<typename T> ostream& operator<<(ostream& out, vector<T> v) { out<<"[ "; loop(i, SZ(v)) { if(i) out<<", "; out<<v[i]; } out<<" ]"; return out; } template<typename T1, typename T2> ostream& operator<<(ostream &out, pair<T1, T2> p) { out<<"( "<<p.fr<<", "<<p.sc<<")"; return out; } template<typename T> ostream& operator,(ostream &out, T x) { out<<x<<" "; return out; } /**********Hasib Templates Ends Here**************/ /** * WARTNING for me: * Never use FOR * Never use pii, pll, vi, vi * Never use ff, ss, phl, sp, nl */ int N; vector<int>v; #define NODE 107 int mark[NODE]; struct KUHN{ int left[NODE], right[NODE], vis[NODE], cc; vector<int>adj[NODE]; KUHN() : cc(1) {} void clear(int n) { FOR(i, 0, n) adj[i].clear(); } bool tryK(int v) { if (vis[v] == cc) return false; vis[v] = cc; loop(i, SZ(adj[v])) { int t = adj[v][i]; if(mark[t]) continue; if(right[t] == -1) { right[t] = v; left[v] = t; return true; } } loop(i, SZ(adj[v])) { int t = adj[v][i]; if(mark[t]) continue; if(tryK(right[t])) { right[t] = v; left[v] = t; return true; } } return false; } int match(int n) { int res = 0; bool done; CLR(left, -1); CLR(right, -1); do{ done = true; cc++; FOR(i, 0, n) { if(mark[i]) continue; if(left[i] == -1 && tryK(i)) { done = false; } } }while(!done); FOR(i, 0, n) { if(mark[i]) continue; res += (left[i] != -1); } return res; } }kuhn; void solve() { sort(all(v)); UNIQUE(v); N = SZ(v); kuhn.clear(N + 2); loop(i, N) { for(int j=i+1; j<N; j++) { if( (v[j] % v[i]) == 0 ) { //vdump(MP(i,j)); kuhn.adj[i].pb(j); } } } mem(mark, 0); int M = N - kuhn.match(N+2); vector<int>result; int m; int prevMarked = 0; loop(i, N) { if(mark[i]) continue; m = 0; for(int j=i+1; j<N; j++) { if( (mark[j] == 0) && (v[j] % v[i] == 0) ) { mark[j] = 2; m++; } } int tmp = (N - m - prevMarked - kuhn.match(N+2)); if(tmp == M ) { result.pb(i); prevMarked += m; m = 1; } else { m = 0; } loop(i, N) { if(mark[i] == 2) { mark[i] = m; } } } loop(i, M) { pf(" %d", v[result[i]]); } pf("\n"); } int main () { #ifdef hasibpc read("input.txt"); //write("output.txt"); #endif // hasibpc int kases, kaseno = 0; sf("%d", &kases); while(kases--) { sf("%d", &N); v.resize(N); loop(i, N) { sf("%d", &v[i]); } pf("Case %d:", ++kaseno); solve(); } return 0; }
(LightOj) 1291 – Real Life Traffic
0/**************************************************************** ▄█ █▄ ▄████████ ▄████████ ▄█ ▀█████████▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ █▀ ███ ███ ███ ▄███▄▄▄▄███▄▄ ███ ███ ███ ███ ▄███▄▄▄██▀ ▀▀███▀▀▀▀███▀ ▀███████████ ▀███████████ ███ ▀▀███▀▀▀██▄ ███ ███ ███ ███ ███ ███ ███ ██▄ ███ ███ ███ ███ ▄█ ███ ███ ███ ███ ███ █▀ ███ █▀ ▄████████▀ █▀ ▄█████████▀ ****************************************************************/ #include<bits/stdc++.h> #define FOR(i, s, e) for(int i=s; i<e; i++) #define loop(i, n) FOR(i, 0, n) #define sf scanf #define pf printf #define pb push_back #define MP make_pair #define fr first #define sc second #define ll long long #define dd double #define all(v) v.begin(), v.end() #define PI acos(-1.0) #define mem(ara, value) memset(ara, value, sizeof(ara)) #define paii pair<int, int> #define pall pair<ll, ll> #define SZ(a) int(a.size()) #define read(nm) freopen(nm, "r", stdin) #define write(nm) freopen(nm, "w", stdout) #define take(args...) asdf,args #define dump(x) cerr<<#x<<" = "<<x<<endl #define debug(args...) cerr,args; cerr<<endl; using namespace std; template<typename T> ostream& operator<<(ostream& output, vector<T>&v) { output<<"[ "; if(SZ(v)) { output<<v[0]; } FOR(i, 1, SZ(v)) { output<<", "<<v[i]; } output<<" ]"; return output; } template<typename T1, typename T2> ostream& operator<<(ostream& output, pair<T1, T2>&p) { output<<"( "<<p.fr<<", "<<p.sc<<" )"; return output; } template<typename T> ostream& operator,(ostream& output, T x) { output<<x<<" "; return output; } struct ASDF{ ASDF& operator,(int &a) { sf("%d", &a); return *this; } ASDF& operator,(long int &a){ sf("%ld", &a); return *this; } ASDF& operator,(long long int &a){ sf("%lld", &a); return *this; } ASDF& operator,(char &c){ sf("%c", &c); return *this; } ASDF& operator,(double &d){ sf("%lf", &d); return *this; } template<typename T> ASDF& operator,(T &a){ cin>>a; return *this; } }asdf; //Header ends here #define MAXX 10007 vector<int>graph[MAXX]; int n, m; map<int, map<int, int> > isBridge; bool visited[MAXX]; int low[MAXX], disc[MAXX]; int parent[MAXX]; int t; vector<paii>bridges; int cnt[MAXX]; void dfs_bridge(int u) { visited[u] = true; low[u] = disc[u] = ++t; loop(i, SZ(graph[u])) { int v = graph[u][i]; if( ! visited[v]) { parent[v] = u; dfs_bridge(v); low[u] = min(low[u], low[v]); if(low[v] > disc[u] ) { bridges.pb(MP(u, v)); isBridge[u][v] = isBridge[v][u] = 1; } } else if( v != parent[u]) { low[u] = min(low[u], disc[v]); } } } int find(int u) { if(u == parent[u]) return u; else return parent[u] = find(parent[u]); } int main() { int kases, kaseno = 0; take(kases); while(kases--) { loop(i, MAXX) graph[i].clear(); isBridge.clear(); take(n, m); int u, v; loop(i, m) { take(u, v); graph[u].pb(v); graph[v].pb(u); } mem(visited, 0); mem(parent, -1); t = 0; bridges.clear(); loop(i, n) { if(!visited[i]) { dfs_bridge(i); } } loop(i, n) { parent[i] = i; } loop(i, n) { loop(j, SZ(graph[i])) { int v = graph[i][j]; if(isBridge[i][v] != 1) { //cerr<<"i is "<<i<<" and v = "<<v<<" "<<isBridge[u][v]<<endl; int u = find(i); v = find(v); if( u != v) { parent[u] = v; //cerr<<"connecting "<<u<<" and "<<v<<endl; } } else { //dump(u); dump(v); //cerr<<"here"<<endl; } } } mem(cnt, 0); loop(i, SZ(bridges)) { int u = bridges[i].fr, v = bridges[i].sc; u = find(u); v = find(v); //dump(u); dump(v); cnt[u]++; cnt[v]++; } int ret = 0; loop(i, n) { if(cnt[i] == 1) { ret++; } } //ret = count of leaf ret = (ret + 1)/2; pf("Case %d: %d\n", ++kaseno, ret); } }
(LightOj) 1063 – Ant Hills
0// link: http://lightoj.com/volume_showproblem.php?problem=1063 // tuto: http://www.geeksforgeeks.org/articulation-points-or-cut-vertices-in-a-graph/ /**************************************************************** ▄█ █▄ ▄████████ ▄████████ ▄█ ▀█████████▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ █▀ ███ ███ ███ ▄███▄▄▄▄███▄▄ ███ ███ ███ ███ ▄███▄▄▄██▀ ▀▀███▀▀▀▀███▀ ▀███████████ ▀███████████ ███ ▀▀███▀▀▀██▄ ███ ███ ███ ███ ███ ███ ███ ██▄ ███ ███ ███ ███ ▄█ ███ ███ ███ ███ ███ █▀ ███ █▀ ▄████████▀ █▀ ▄█████████▀ ****************************************************************/ #include<bits/stdc++.h> #define FOR(i, s, e) for(int i=s; i<e; i++) #define loop(i, n) FOR(i, 0, n) #define sf scanf #define pf printf #define pb push_back #define MP make_pair #define fr first #define sc second #define ll long long #define dd double #define all(v) v.begin(), v.end() #define PI acos(-1.0) #define mem(ara, value) memset(ara, value, sizeof(ara)) #define paii pair<int, int> #define pall pair<ll, ll> #define SZ(a) int(a.size()) #define read(nm) freopen(nm, "r", stdin) #define write(nm) freopen(nm, "w", stdout) #define take(args...) asdf,args #define dump(x) cerr<<#x<<" = "<<x<<endl #define debug(args...) cerr,args; cerr<<endl; using namespace std; template<typename T> ostream& operator<<(ostream& output, vector<T>&v) { output<<"[ "; if(SZ(v)) { output<<v[0]; } FOR(i, 1, SZ(v)) { output<<", "<<v[i]; } output<<" ]"; return output; } template<typename T1, typename T2> ostream& operator<<(ostream& output, pair<T1, T2>&p) { output<<"( "<<p.fr<<", "<<p.sc<<" )"; return output; } template<typename T> ostream& operator,(ostream& output, T x) { output<<x<<" "; return output; } struct ASDF{ ASDF& operator,(int &a) { sf("%d", &a); return *this; } ASDF& operator,(long int &a){ sf("%ld", &a); return *this; } ASDF& operator,(long long int &a){ sf("%lld", &a); return *this; } ASDF& operator,(char &c){ sf("%c", &c); return *this; } ASDF& operator,(double &d){ sf("%lf", &d); return *this; } template<typename T> ASDF& operator,(T &a){ cin>>a; return *this; } }asdf; //Header ends here #define MAXX 10007 int N, M; vector<int>adj[MAXX]; bool visited[MAXX]; int parent[MAXX]; int low[MAXX]; int disc[MAXX]; int tyme; bool isArticulationPoint[MAXX]; void init() { loop(i, MAXX) { adj[i].clear(); visited[i] = false; parent[i] = -1; isArticulationPoint[i] = false; } tyme = 0; } void dfs_articulation_point(int u) { visited[u] = true; disc[u] = low[u] = ++tyme; int cntChild = 0; loop(i, SZ(adj[u])) { int v = adj[u][i]; if( ! visited[v] ) { cntChild++; parent[v] = u; dfs_articulation_point(v); low[u] = min(low[u], low[v]); if(parent[u] != -1) { if(low[v] >= disc[u]) { isArticulationPoint[u] = true; } } } else if(v != parent[u]) { low[u] = min(low[u], disc[v]); } } if(parent[u] == -1) { if(cntChild > 1) { isArticulationPoint[u] = true; } } } int main() { int kases, kaseno = 0; take(kases); while(kases--) { take(N, M); init(); int u, v; loop(i, M) { take(u, v); adj[u].pb(v); adj[v].pb(u); } for(int i=1; i<=N; i++) { if( ! visited[i] ) { dfs_articulation_point(i); } } int cntArticulationPoint = 0; for(int i=1; i<=N; i++) { if(isArticulationPoint[i]) cntArticulationPoint++; } pf("Case %d: %d\n",++kaseno, cntArticulationPoint); } }
(LighOj) 1026 – Critical Links
0//link: http://lightoj.com/volume_showproblem.php?problem=1026 //tuto: http://www.geeksforgeeks.org/bridge-in-a-graph/ /**************************************************************** ▄█ █▄ ▄████████ ▄████████ ▄█ ▀█████████▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ █▀ ███ ███ ███ ▄███▄▄▄▄███▄▄ ███ ███ ███ ███ ▄███▄▄▄██▀ ▀▀███▀▀▀▀███▀ ▀███████████ ▀███████████ ███ ▀▀███▀▀▀██▄ ███ ███ ███ ███ ███ ███ ███ ██▄ ███ ███ ███ ███ ▄█ ███ ███ ███ ███ ███ █▀ ███ █▀ ▄████████▀ █▀ ▄█████████▀ ****************************************************************/ #include<bits/stdc++.h> #define FOR(i, s, e) for(int i=s; i<e; i++) #define loop(i, n) FOR(i, 0, n) #define sf scanf #define pf printf #define pb push_back #define MP make_pair #define fr first #define sc second #define ll long long #define dd double #define all(v) v.begin(), v.end() #define PI acos(-1.0) #define mem(ara, value) memset(ara, value, sizeof(ara)) #define paii pair<int, int> #define pall pair<ll, ll> #define SZ(a) int(a.size()) #define read(nm) freopen(nm, "r", stdin) #define write(nm) freopen(nm, "w", stdout) #define take(args...) asdf,args #define dump(x) cerr<<#x<<" = "<<x<<endl #define debug(args...) cerr,args; cerr<<endl; using namespace std; template<typename T> ostream& operator<<(ostream& output, vector<T>&v) { output<<"[ "; if(SZ(v)) { output<<v[0]; } FOR(i, 1, SZ(v)) { output<<", "<<v[i]; } output<<" ]"; return output; } template<typename T1, typename T2> ostream& operator<<(ostream& output, pair<T1, T2>&p) { output<<"( "<<p.fr<<", "<<p.sc<<" )"; return output; } template<typename T> ostream& operator,(ostream& output, T x) { output<<x<<" "; return output; } struct ASDF{ ASDF& operator,(int &a) { sf("%d", &a); return *this; } ASDF& operator,(long int &a){ sf("%ld", &a); return *this; } ASDF& operator,(long long int &a){ sf("%lld", &a); return *this; } ASDF& operator,(char &c){ sf("%c", &c); return *this; } ASDF& operator,(double &d){ sf("%lf", &d); return *this; } template<typename T> ASDF& operator,(T &a){ cin>>a; return *this; } }asdf; //Header ends here #define MAXX 10007 int n; vector<int>adj[MAXX]; int tyme; bool visited[MAXX]; int parent[MAXX]; int low[MAXX]; int disc[MAXX]; vector<paii>bridges; void dfs_bridge(int u) { visited[u] = true; disc[u] = low[u] = ++tyme; int child = 0; loop(i, SZ(adj[u])) { int v = adj[u][i]; if( ! visited[v]) { parent[v] = u; dfs_bridge(v); low[u] = min(low[u], low[v]); if( low[v] > disc[u] ) { bridges.pb(min(MP(u, v), MP(v, u))); } } else if( v != parent[u] ) { low[u] = min(low[u], disc[v]); } } } int main() { int kases, kaseno = 0; sf("%d", &kases); while(kases--) { sf("%d", &n); int u, k, v; char ch; loop(i, n) { adj[i].clear(); parent[i] = -1; visited[i] = false; } bridges.clear(); loop(i, n) { sf("%d %c%d%c", &u, &ch, &k, &ch); loop(j, k) { sf("%d", &v); adj[u].pb(v); } } loop(i, n) { if( ! visited[i] ) { dfs_bridge(i); } } pf("Case %d:\n", ++kaseno); pf("%d critical links\n", SZ(bridges)); sort(all(bridges)); loop(i, SZ(bridges)) { pf("%d - %d\n", bridges[i].fr, bridges[i].sc); } } }
(LightOj) 1141 – Number Transformation
0/**************************************************************** ▄█ █▄ ▄████████ ▄████████ ▄█ ▀█████████▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ █▀ ███ ███ ███ ▄███▄▄▄▄███▄▄ ███ ███ ███ ███ ▄███▄▄▄██▀ ▀▀███▀▀▀▀███▀ ▀███████████ ▀███████████ ███ ▀▀███▀▀▀██▄ ███ ███ ███ ███ ███ ███ ███ ██▄ ███ ███ ███ ███ ▄█ ███ ███ ███ ███ ███ █▀ ███ █▀ ▄████████▀ █▀ ▄█████████▀ ****************************************************************/ #include<bits/stdc++.h> #define FOR(i, s, e) for(int i=s; i<e; i++) #define loop(i, n) FOR(i, 0, n) #define sf scanf #define pf printf #define pb push_back #define MP make_pair #define fr first #define sc second #define ll long long #define dd double #define all(v) v.begin(), v.end() #define PI acos(-1.0) #define mem(ara, value) memset(ara, value, sizeof(ara)) #define paii pair<int, int> #define pall pair<ll, ll> #define SZ(a) int(a.size()) #define read(nm) freopen(nm, "r", stdin) #define write(nm) freopen(nm, "w", stdout) #define take(args...) asdf,args #define dump(x) cerr<<#x<<" = "<<x<<endl #define debug(args...) cerr,args; cerr<<endl; using namespace std; template<typename T> ostream& operator<<(ostream& output, vector<T>&v) { output<<"[ "; if(SZ(v)) { output<<v[0]; } FOR(i, 1, SZ(v)) { output<<", "<<v[i]; } output<<" ]"; return output; } template<typename T1, typename T2> ostream& operator<<(ostream& output, pair<T1, T2>&p) { output<<"( "<<p.fr<<", "<<p.sc<<" )"; return output; } template<typename T> ostream& operator,(ostream& output, T x) { output<<x<<" "; return output; } struct ASDF{ ASDF& operator,(int &a) { sf("%d", &a); return *this; } ASDF& operator,(long int &a){ sf("%ld", &a); return *this; } ASDF& operator,(long long int &a){ sf("%lld", &a); return *this; } ASDF& operator,(char &c){ sf("%c", &c); return *this; } ASDF& operator,(double &d){ sf("%lf", &d); return *this; } template<typename T> ASDF& operator,(T &a){ cin>>a; return *this; } }asdf; //Header ends here #define MAXX 1007 vector<int>factors[MAXX]; vector<int>primes; void generatePrimes() { bool isPrime[MAXX]; mem(isPrime, 1); int root = sqrt(MAXX) + 7; for(int i=3; i<root; i++) { if(isPrime[i]) { for(int j=i*i; j<MAXX; j+=(2*i)) { isPrime[j] = false; } } } for(int i=2; i<MAXX;) { if(isPrime[i]) { for(int j=2; i*j < MAXX; j++) { factors[i*j].pb(i); } } if(i == 2) { i++; } else { i += 2; } } } void init() { generatePrimes(); } int bfs(int source, int target) { bool visited[MAXX]; int dist[MAXX]; mem(dist, -1); mem(visited, 0); queue<int>Q; Q.push(source); visited = true; dist = 0; while(!Q.empty()) { int u = Q.front(); Q.pop(); loop(i, SZ(factors[u])) { int v = u + factors[u][i]; if( (v <= target) && !visited[v] ) { visited[v] = true; dist[v] = dist[u] + 1; if(v == target) { break; } Q.push(v); } } } return dist[target]; } int main() { init(); int kases, kaseno = 0; int s, t; sf("%d", &kases); while(kases--) { sf("%d %d", &s, &t); pf("Case %d: %d\n", ++kaseno, bfs(s, t)); } return 0; }
(LightOj) 1154 – Penguins
0/**************************************************************** ▄█ █▄ ▄████████ ▄████████ ▄█ ▀█████████▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ █▀ ███ ███ ███ ▄███▄▄▄▄███▄▄ ███ ███ ███ ███ ▄███▄▄▄██▀ ▀▀███▀▀▀▀███▀ ▀███████████ ▀███████████ ███ ▀▀███▀▀▀██▄ ███ ███ ███ ███ ███ ███ ███ ██▄ ███ ███ ███ ███ ▄█ ███ ███ ███ ███ ███ █▀ ███ █▀ ▄████████▀ █▀ ▄█████████▀ ****************************************************************/ #include<bits/stdc++.h> #define FOR(i, s, e) for(int i=s; i<e; i++) #define loop(i, n) FOR(i, 0, n) #define sf scanf #define pf printf #define pb push_back #define MP make_pair #define fr first #define sc second #define ll long long #define dd double #define all(v) v.begin(), v.end() #define PI acos(-1.0) #define mem(ara, value) memset(ara, value, sizeof(ara)) #define paii pair<int, int> #define pall pair<ll, ll> #define SZ(a) int(a.size()) #define read(nm) freopen(nm, "r", stdin) #define write(nm) freopen(nm, "w", stdout) #define take(args...) asdf,args #define dump(x) cerr<<#x<<" = "<<x<<endl #define debug(args...) cerr,args; cerr<<endl; using namespace std; template<typename T> ostream& operator<<(ostream& output, vector<T>&v) { output<<"[ "; if(SZ(v)) { output<<v[0]; } FOR(i, 1, SZ(v)) { output<<", "<<v[i]; } output<<" ]"; return output; } template<typename T1, typename T2> ostream& operator<<(ostream& output, pair<T1, T2>&p) { output<<"( "<<p.fr<<", "<<p.sc<<" )"; return output; } template<typename T> ostream& operator,(ostream& output, T x) { output<<x<<" "; return output; } struct ASDF{ ASDF& operator,(int &a) { sf("%d", &a); return *this; } ASDF& operator,(long int &a){ sf("%ld", &a); return *this; } ASDF& operator,(long long int &a){ sf("%lld", &a); return *this; } ASDF& operator,(char &c){ sf("%c", &c); return *this; } ASDF& operator,(double &d){ sf("%lf", &d); return *this; } template<typename T> ASDF& operator,(T &a){ cin>>a; return *this; } }asdf; //Header ends here #define MAXX 208 #define INF (1<<29) int capacity[MAXX][MAXX], tmpCapacity[MAXX][MAXX]; int N; double D; pair<double, double>points[MAXX/2]; vector<int>graph[MAXX]; int source, sink; int total; double sq(double x) { return x*x; } double dist(pair<double, double>A, pair<double, double>B) { return sqrt( sq(A.fr - B.fr) + sq(A.sc - B.sc) ); } int findPath() { bool visited[MAXX]; bool found = false; int parent[MAXX]; mem(parent, -1); mem(visited, 0); queue<int>Q; Q.push(source); visited = true; while(!Q.empty()) { int u = Q.front(); Q.pop(); loop(i, SZ(graph[u])) { int v = graph[u][i]; if(!visited[v] && tmpCapacity[u][v] > 0) { visited[v] = true; parent[v] = u; Q.push(v); if(v == sink) { found = true; break; } } } if(found) { break; } } int pathCapacity = INF; int u, v = sink; while(parent[v] != -1) { u = parent[v]; pathCapacity = min(pathCapacity, tmpCapacity[u][v]); v = u; } v = sink; while(parent[v] != -1) { u = parent[v]; tmpCapacity[u][v] -= pathCapacity; tmpCapacity[v][u] += pathCapacity; v = u; } return found?pathCapacity:0; } int FLOW() { loop(i, MAXX) { loop(j, MAXX) { tmpCapacity[i][j] = capacity[i][j]; } } int ret = 0; int pathCapacity; while(pathCapacity = findPath()) { ret += pathCapacity; } return ret; } void init() { } int main() { init(); int kases, kaseno = 0; int n, c; int u, v; sf("%d", &kases); while(kases--) { mem(capacity, 0); loop(i, MAXX) graph[i].clear(); total = 0; sf("%d %lf", &N, &D); source = 0; for(int i=1; i<=N; i++) { sf("%lf %lf %d %d", &points[i].fr, &points[i].sc, &n, &c); total += n; v = 2*i; u = v - 1; graph[u].pb(v); graph[v].pb(u); capacity[u][v] = c; graph.pb(u); graph[u].pb(source); capacity[u] = n; } for(int i=1; i<=N; i++) { for(int j=1; j<=N; j++) { if(i == j) continue; if(dist(points[i], points[j]) <= D) { u = 2*i; v = 2*j - 1; capacity[u][v] = INF; graph[u].pb(v); graph[v].pb(u); } } } vector<int>vv; for(int i=1; i<=N; i++) { sink = 2*i - 1; if(FLOW() == total) { vv.pb(i); } } printf("Case %d:", ++kaseno); if(SZ(vv) == 0) { printf(" -1"); } else { loop(i, SZ(vv)) { printf(" %d", vv[i] - 1); } } pf("\n"); } return 0; }
(LightOj) 1155 – Power Transmission
0/**************************************************************** ▄█ █▄ ▄████████ ▄████████ ▄█ ▀█████████▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ █▀ ███ ███ ███ ▄███▄▄▄▄███▄▄ ███ ███ ███ ███ ▄███▄▄▄██▀ ▀▀███▀▀▀▀███▀ ▀███████████ ▀███████████ ███ ▀▀███▀▀▀██▄ ███ ███ ███ ███ ███ ███ ███ ██▄ ███ ███ ███ ███ ▄█ ███ ███ ███ ███ ███ █▀ ███ █▀ ▄████████▀ █▀ ▄█████████▀ ****************************************************************/ #include<bits/stdc++.h> #define FOR(i, s, e) for(int i=s; i<e; i++) #define loop(i, n) FOR(i, 0, n) #define sf scanf #define pf printf #define pb push_back #define MP make_pair #define fr first #define sc second #define ll long long #define dd double #define all(v) v.begin(), v.end() #define PI acos(-1.0) #define mem(ara, value) memset(ara, value, sizeof(ara)) #define paii pair<int, int> #define pall pair<ll, ll> #define SZ(a) int(a.size()) #define read(nm) freopen(nm, "r", stdin) #define write(nm) freopen(nm, "w", stdout) #define take(args...) asdf,args #define dump(x) cerr<<#x<<" = "<<x<<endl #define debug(args...) cerr,args; cerr<<endl; using namespace std; template<typename T> ostream& operator<<(ostream& output, vector<T>&v) { output<<"[ "; if(SZ(v)) { output<<v[0]; } FOR(i, 1, SZ(v)) { output<<", "<<v[i]; } output<<" ]"; return output; } template<typename T1, typename T2> ostream& operator<<(ostream& output, pair<T1, T2>&p) { output<<"( "<<p.fr<<", "<<p.sc<<" )"; return output; } template<typename T> ostream& operator,(ostream& output, T x) { output<<x<<" "; return output; } struct ASDF{ ASDF& operator,(int &a) { sf("%d", &a); return *this; } ASDF& operator,(long int &a){ sf("%ld", &a); return *this; } ASDF& operator,(long long int &a){ sf("%lld", &a); return *this; } ASDF& operator,(char &c){ sf("%c", &c); return *this; } ASDF& operator,(double &d){ sf("%lf", &d); return *this; } template<typename T> ASDF& operator,(T &a){ cin>>a; return *this; } }asdf; //Header ends here #define MAXX 207 #define INF (1<<29) int source, sink; int N; int M; int capacity[MAXX][MAXX]; vector<int>graph[MAXX]; int findPath() { bool visited[MAXX]; int parent[MAXX]; mem(parent, -1); mem(visited, 0); queue<int>Q; Q.push(source); visited = true; bool found = false; while(!Q.empty()) { int u = Q.front(); Q.pop(); loop(i, SZ(graph[u])) { int v = graph[u][i]; if(!visited[v] && capacity[u][v] > 0) { visited[v] = true; Q.push(v); parent[v] = u; if(v == sink) { found = true; break; } } } if(found) { break; } } int v = sink; int pathCapacity = INF; while(parent[v] != -1) { int u = parent[v]; pathCapacity = min(pathCapacity, capacity[u][v] ); v = u; } v = sink; while(parent[v] != -1) { int u = parent[v]; capacity[u][v] -= pathCapacity; capacity[v][u] += pathCapacity; v = u; } if(found) { return pathCapacity; } else { return 0; } } int FLOW() { int result = 0; int pathCapacity; while(true) { pathCapacity = findPath(); if(pathCapacity == 0) { break; } else { result += pathCapacity; } } return result; } int solve() { return FLOW(); } void init() { } int main() { init(); int kases, kaseno = 0; int B, D; int u, v, c; sf("%d", &kases); while(kases--) { mem(capacity, 0); sf("%d", &N); source = 0; sink = 2*(N + 1); // A number out of the range of node id. for(int i=0; i<=sink; i++) { graph[i].clear(); } for(int i=1; i<=N; i++) { u = 2*i - 1; v = u + 1; graph[u].pb(v); graph[v].pb(u); sf("%d", &capacity[u][v]); } sf("%d", &M); loop(i, M) { sf("%d %d %d", &u, &v, &c); u = 2*u; v = 2*v - 1; graph[u].pb(v); graph[v].pb(u); capacity[u][v] = c; } sf("%d %d", &B, &D); loop(i, B) { sf("%d", &u); u = 2*u - 1; graph.pb(u); graph[u].pb(source); capacity[ source ][u] = INF; } loop(i, D) { sf("%d", &v); { v = 2*v; graph[v].pb(sink); graph[sink].pb(v); capacity[v][sink] = INF; } } pf("Case %d: %d\n", ++kaseno, solve()); } return 0; }
(UVa) 615 – Is It A Tree?
0/**************************************************************** ▄█ █▄ ▄████████ ▄████████ ▄█ ▀█████████▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ █▀ ███ ███ ███ ▄███▄▄▄▄███▄▄ ███ ███ ███ ███ ▄███▄▄▄██▀ ▀▀███▀▀▀▀███▀ ▀███████████ ▀███████████ ███ ▀▀███▀▀▀██▄ ███ ███ ███ ███ ███ ███ ███ ██▄ ███ ███ ███ ███ ▄█ ███ ███ ███ ███ ███ █▀ ███ █▀ ▄████████▀ █▀ ▄█████████▀ ****************************************************************/ #include<bits/stdc++.h> #define FOR(i, s, e) for(int i=s; i<e; i++) #define loop(i, n) FOR(i, 0, n) #define sf scanf #define pf printf #define pb push_back #define MP make_pair #define fr first #define sc second #define ll long long #define dd double #define all(v) v.begin(), v.end() #define PI acos(-1.0) #define mem(ara, value) memset(ara, value, sizeof(ara)) #define paii pair<int, int> #define pall pair<ll, ll> #define SZ(a) int(a.size()) #define read(nm) freopen(nm, "r", stdin) #define write(nm) freopen(nm, "w", stdout) #define take(args...) asdf,args #define dump(x) cerr<<#x<<" = "<<x<<endl #define debug(args...) cerr,args; cerr<<endl; using namespace std; template<typename T> ostream& operator<<(ostream& output, vector<T>&v) { output<<"[ "; if(SZ(v)) { output<<v[0]; } FOR(i, 1, SZ(v)) { output<<", "<<v[i]; } output<<" ]"; return output; } template<typename T1, typename T2> ostream& operator<<(ostream& output, pair<T1, T2>&p) { output<<"( "<<p.fr<<", "<<p.sc<<" )"; return output; } template<typename T> ostream& operator,(ostream& output, T x) { output<<x<<" "; return output; } struct ASDF{ ASDF& operator,(int &a) { sf("%d", &a); return *this; } ASDF& operator,(long int &a){ sf("%ld", &a); return *this; } ASDF& operator,(long long int &a){ sf("%lld", &a); return *this; } ASDF& operator,(char &c){ sf("%c", &c); return *this; } ASDF& operator,(double &d){ sf("%lf", &d); return *this; } template<typename T> ASDF& operator,(T &a){ cin>>a; return *this; } }asdf; //Header ends here set<int>NodeList; set<int>inputNodes; map<int,int>parent; int find(int u) { if(parent[u] == u) return u; return parent[u] = find(parent[u]); } void init() { } int main() { init(); int kaseno = 0; int p, q; int edge; bool possible; while(true) { scanf("%d %d", &p, &q); if(p < 0 && q < 0) { break; } else { edge = 0; NodeList.clear(); inputNodes.clear(); parent.clear(); possible = true; while(p != 0 && q != 0) { edge++; NodeList.insert(p); NodeList.insert(q); if(parent[p] == 0) { parent[p] = p; } if(parent[q] == 0) { parent[q] = q; } int u = find(p); int v = find(q); if(u == v) { possible = false; } parent[ v ] = u; if(inputNodes.find(q) != inputNodes.end()) { possible = false; } else { inputNodes.insert(q); } scanf("%d %d", &p, &q); } if( edge == 0 || (possible && (edge == SZ(NodeList) - 1) && (edge == SZ(inputNodes)) ) ) { pf("Case %d is a tree.\n", ++kaseno); } else { pf("Case %d is not a tree.\n", ++kaseno); } } } return 0; }
D. Hydra (codeforces)
0//link: http://codeforces.com/contest/244/problem/D #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<vector> #include<queue> #include<stack> #include<map> #include<utility> #include<set> #define FOR(i, s, e) for(int i=s; i<e; i++) #define loop(i, n) FOR(i, 0, n) #define sf scanf #define pf printf #define pb push_back #define MP make_pair #define fr first #define sc second #define ll long long #define dd double #define all(v) v.begin(), v.end() #define PI acos(-1.0) #define mem(ara, value) memset(ara, value, sizeof(ara)) #define paii pair<int, int> #define pall pair<ll, ll> #define SZ(a) int(a.size()) #define read(nm) freopen(nm, "r", stdin) #define write(nm) freopen(nm, "w", stdout) #define dump(x) cout<<#x<<" = "<<x<<endl using namespace std; #define take(args...) asdf,args #define debug(args...) asdfg,args; cout<<endl struct ASDF{ ASDF& operator,(int &a) { sf("%d", &a); return *this; } ASDF& operator,(long int &a){ sf("%ld", &a); return *this; } ASDF& operator,(long long int &a){ sf("%lld", &a); return *this; } ASDF& operator,(char &c){ sf("%c", &c); return *this; } ASDF& operator,(double &d){ sf("%lf", &d); return *this; } template<typename T> ASDF& operator,(T &a){ cin>>a; return *this; } }asdf; struct ASDFG{ template<typename T> ASDFG& operator,(vector<T> &v){ pf("["); cout<<v[0]; FOR(i, 1, SZ(v)){ cout<<", "<<v[i]; } pf("]"); return *this; } template<typename T> ASDFG& operator,(T x) { cout<<x<<" "; return *this; } }asdfg; //Header ends here #define MAXX 100004 int deg[MAXX]; vector<paii> edges; int h, t; vector<int>graph[MAXX]; int isAchived[MAXX]; int isValid(int u, int v) { if(deg[u] < h+1 || deg[v] < t+1 ) { return 0; } loop(i, SZ(graph[u])) { isAchived[ graph[u][i] ]++; } int duplicate = 0; loop(i, SZ(graph[v])) { if(isAchived[ graph[v][i] ]) { duplicate++; isAchived[graph[v][i] ]++; } } if(deg[u] + deg[v] - duplicate - 2 >= h+t) { return 1; } else { loop(i, SZ(graph[u])) isAchived[ graph[u][i] ] = 0; return 0; } } void generateSolution(int u, int v) { vector<int>v1, v2, com; loop(i, SZ(graph[u])) { if(graph[u][i] == v) continue; if(isAchived[ graph[u][i] ] == 1 ) { v1.pb( graph[u][i] ); } else { com.pb(graph[u][i]); } } loop(i, SZ(graph[v])) { if(graph[v][i] == u) continue; if( isAchived[ graph[v][i] ] == 0 ) { v2.pb(graph[v][i]); } } int pos = 0; while(SZ(v1) < h) { v1.pb(com[pos++]); } while(SZ(v2) < t) { v2.pb(com[pos++]); } pf("YES\n"); pf("%d %d\n", u, v); pf("%d", v1[0]); FOR(i,1,h) { pf(" %d", v1[i]); } pf("\n"); pf("%d", v2[0]); FOR(i,1,t) { pf(" %d", v2[i]); } pf("\n"); } int main() { int n, m, u, v; take(n, m, h, t); loop(i, m) { take(u, v); edges.pb(MP(u, v)); deg[u]++; deg[v]++; graph[u].pb(v); graph[v].pb(u); } bool possible = false; loop(i, m) { if(isValid(edges[i].fr, edges[i].sc)) { possible = 1; generateSolution(edges[i].fr, edges[i].sc); break; } else if(isValid(edges[i].sc, edges[i].fr)) { possible=1; generateSolution(edges[i].sc, edges[i].fr); break; } } if(!possible) cout<<"NO\n"; }