/***********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 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; } }; vector<int>original[NODE], modified[NODE]; int orgN, modN; bool orgVisited[NODE], modVisited[NODE]; bool isMatch[NODE][NODE]; bool match(int u, int v) { orgVisited[u] = modVisited[v] = true; int uu, vv; KUHN kuhn; /// !wow! Got WA because I took kuhn as local variable, and didn't clear visited! mem(kuhn.vis, 0); kuhn.clear(NODE-1); loop(i, SZ(original[u])) { uu = original[u][i]; if(orgVisited[uu]) continue; loop(j, SZ(modified[v])) { vv = modified[v][j]; if(modVisited[vv]) continue; if(match(uu, vv)) { kuhn.adj[i].pb(j); } } } orgVisited[u] = modVisited[v] = false; int m = kuhn.match( SZ(original[u]) + 2 ); int child = SZ(original[u]) - 1; if(u == 1) child++; bool ret = (m == child); return ret; } void solve() { //mem(orgVisited, 0); /// //mem(modVisited, 0); /// bool ret = match(1, 1); if(ret) { pf("Yes\n"); } else { pf("No\n"); } } int main () { #ifdef hasibpc read("input.txt"); //write("output.txt"); #endif // hasibpc int kases, kaseno = 0; int u, v; sf("%d", &kases); while(kases--) { sf("%d", &modN); FOR(i, 0, modN) modified[i].clear(); for(int i=1; i<modN; i++) { sf("%d %d", &u, &v); //debug(u, v); modified[u].pb(v); modified[v].pb(u); } //vdump(endl); sf("%d", &orgN); FOR(i, 0, orgN) original[i].clear(); for(int i=1; i<orgN; i++) { sf("%d %d", &u, &v); //debug(u, v); original[u].pb(v); original[v].pb(u); } pf("Case %d: ", ++kaseno); solve(); } return 0; }
Category Archives: DFS
(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); } } }
(UVa) 572 – Oil Deposits
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 107 int rows, columns; char graph[MAXX][MAXX]; bool visited[MAXX][MAXX]; int component; int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; void dfs(paii u) { visited[u.fr][u.sc] = true; paii v; loop(i, 8) { v = MP(u.fr + dx[i], u.sc + dy[i]); if(0<=v.fr && v.fr <rows && 0<= v.sc && v.sc <columns) { if( ! visited[v.fr][v.sc] && graph[v.fr][v.sc] == '@' ) { dfs(v); } } } } void solve() { mem(visited, 0); component = 0; loop(i, rows) { loop(j, columns) { if(!visited[i][j] && graph[i][j] == '@') { component++; dfs(MP(i, j)); } } } } int main() { while(true) { sf("%d %d", &rows, &columns); if(rows == 0) break; loop(i, rows) { sf("%s", graph[i]); } solve(); //dump(component); pf("%d\n", component); } }
(USACO) Humble Numbers
0// link: http://cerberus.delos.com:790/usacoprob2?a=VKy6gbSa7gv&S=humble /* ID: himuhas1 TASK: humble LANG: C++ */ #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 MAX_PRIMES 102 #define MAX_N 100002 #define INF 4611686018427387904 int primes[MAX_PRIMES]; int hprime[MAX_PRIMES]; long long humble[MAX_N]; int main() { #ifndef hasibpc read("humble.in"); write("humble.out"); #endif int cntPrime, N; take(cntPrime, N); long long temp; loop(i, cntPrime) { take(primes[i]); } mem(hprime, 0); humble[0] = 1; loop(i, N) { long long next = INF; loop(p, cntPrime) { while(primes[p]*humble[ hprime[p] ] <= humble[i]) { hprime[p]++; } temp = primes[p]*humble[ hprime[p] ]; if(temp < next) { next = temp; } } humble[i+1] = next; } cout<<humble[N]<<endl; return 0; }
(USACO) Controlling Companies
0/* ID: himuhas1 TASK: concom LANG: C++ */ #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 gi(a) sf("%d", &a) #define gi2(a, b) sf("%d%d", &a, &b) #define gi3(a, b, c) sf("%d %d %d", &a, &b, &c) #define gi4(a, b, c, d) sf("%d%d%d%d", &a, &b, &c, &d) #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 freopen("input.txt", "r", stdin) #define write freopen("output.txt", "w", stdout) using namespace std; #define MAXX 101 vector<paii>graph[MAXX]; set<int>allNodes; vector<int> result; bool visited[MAXX]; int rank[MAXX]; void dfs(int source) { visited = 1; loop(i, SZ(graph)) { if(!visited[graph[i].fr]) { rank[ graph[i].fr ] += graph[i].sc; if(rank[graph[i].fr] > 50) { result.pb(graph[i].fr); dfs(graph[i].fr); } } } } int main() { #ifndef hasibpc freopen("concom.in", "r", stdin); freopen("concom.out", "w", stdout); #endif // hasibpc int connection; int p, q, r; gi(connection); loop(i, connection) { gi3(p, q, r); allNodes.insert(p); allNodes.insert(q); graph[p].pb(MP(q, r)); } for(set<int>::iterator it=allNodes.begin(); it != allNodes.end(); it++) { result.clear(); mem(visited, 0); mem(rank, 0); dfs(*it); sort(all(result)); loop(i, SZ(result)) { pf("%d %d\n", *it, result[i]); } } return 0; }
(UVa) 247 – Calling Circles
0/* user: php link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=183 */ #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<vector> #include<string> #include<stack> #include<queue> #include<map> #include<sstream> #include<utility> #define FOR(i, s, e) for(int i=s; i<e; i++) #define loop(i, n) for(int i=0; i<n; i++) #define getint(n) scanf("%d", &n) #define pb(a) push_back(a) #define ll long long #define dd double #define SZ(a) int(a.size()) #define read() freopen("input.txt", "r", stdin) #define write() freopen("output.txt", "w", stdout) #define mem(a, v) memset(a, v, sizeof(a)) #define all(v) v.begin(), v.end() #define pi acos(-1.0) #define INF 1<<29 #define mod abs #define pf printf #define sf scanf #define mp make_pair #define paii pair<int, int> #define padd pair<dd, dd> #define pall pair<ll, ll> #define fr first #define sc second using namespace std; #define MAXX 27 int n, m; map<string, int>pos; vector<int>graph[MAXX]; string names[MAXX]; bool possible[MAXX][MAXX]; bool used[MAXX]; void dfs(int u, int v) { possible[u][v] = 1; loop(i, SZ(graph[v])) { if(!possible[u][ graph[v][i] ]) { dfs(u, graph[v][i] ); } } } int main() { //read(); int kaseno = 0; int id; int *p, *q; char fname[MAXX], lname[MAXX]; string fffname, lllname; while(1) { getint(n); getint(m); id = 0; if(n+m == 0) break; pos.clear(); for(int i=1; i<=n; i++) graph[i].clear(); loop(i, m) { sf("%s %s", fname, lname); fffname = fname; lllname = lname; p = &pos[fffname]; if(*p == 0) { *p = ++id; names[*p] = fffname; } q = &pos[lllname]; if(*q == 0) { *q = ++id; names[*q] = lllname; } graph[*p].pb(*q); } mem(possible, 0); mem(used, 0); for(int i=1; i<=n; i++) { dfs(i, i); } if(kaseno) pf("\n"); pf("Calling circles for data set %d:\n", ++kaseno); for(int i=1; i<=n; i++) { if(used[i]) continue; used[i] = 1; pf("%s", names[i].c_str() ); for(int j=1; j<=n; j++) { if(!used[j] && possible[i][j] && possible[j][i]) { pf(", %s", names[j].c_str()); //pf(", %d", j); used[j] = 1; } } pf("\n"); } } return 0; }
(UVa) 11709 – Trust groups
0/* user: php link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2756 */ #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<vector> #include<string> #include<stack> #include<queue> #include<map> #include<sstream> #include<utility> #define FOR(i, s, e) for(int i=s; i<e; i++) #define loop(i, n) for(int i=0; i<n; i++) #define getint(n) scanf("%d", &n) #define pb(a) push_back(a) #define ll long long #define dd double #define SZ(a) int(a.size()) #define read() freopen("input.txt", "r", stdin) #define write() freopen("output.txt", "w", stdout) #define mem(a, v) memset(a, v, sizeof(a)) #define all(v) v.begin(), v.end() #define pi acos(-1.0) #define INF 1<<29 #define mod abs #define pf printf #define sf scanf #define mp make_pair #define paii pair<int, int> #define padd pair<dd, dd> #define pall pair<ll, ll> #define fr first #define sc second using namespace std; #define MAXX 1002 map<string, int> mapping; vector<int>graph[MAXX]; bool visited[MAXX][MAXX]; bool used[MAXX]; int P, T; void dfs(int u, int v) { visited[u][v] = 1; int vv; loop(i, SZ(graph[v])) { vv = graph[v][i]; if(!visited[u][vv]) { dfs(u, vv); } } } int main() { //string str1, str2; char finput[25], sinput[25]; int cnt; while(1) { getint(P); getint(T); if(P == 0 && T == 0) break; mapping.clear(); cin.ignore(); loop(i, P) { graph[i].clear(); //getline(cin, str1); gets(finput); mapping[finput] = i; } loop(i, T) { //getline(cin, str1); //getline(cin, str2); gets(finput); gets(sinput); graph[ mapping[finput] ].pb(mapping[sinput]); } mem(visited, 0); loop(i, P) dfs(i, i); mem(used, 0); cnt = 0; loop(i, P) { if(!used[i]) { cnt++; FOR(j, i+1, P) { if( !used[j] && visited[i][j] && visited[j][i] ) { used[j] = 1; } } } } pf("%d\n", cnt); } return 0; }
(UVa) 10308 – Roads in the North
0/* user: php time: 0.304 sec link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1249 */ #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<vector> #include<string> #include<stack> #include<queue> #include<map> #include<sstream> #define FOR(i, s, e) for(int i=s; i<e; i++) #define loop(i, n) for(int i=0; i<n; i++) #define getint(n) scanf("%d", &n) #define pb(a) push_back(a) #define ll long long #define SZ(a) int(a.size()) #define read() freopen("input.txt", "r", stdin) #define write() freopen("output.txt", "w", stdout) #define mem(a, v) memset(a, v, sizeof(a)) #define all(v) v.begin(), v.end() #define pi acos(-1.0) #define INF 1<<29 #define mod(a) (a>0?a:-a) #define pf printf #define sf scanf using namespace std; #define MAXX 10002 vector<int>graph[MAXX]; int cost[MAXX][MAXX]; ll dist[MAXX]; bool visited[MAXX]; int farnode1; ll bfs(int u) { mem(visited, false); queue<int>Q; ll maxdist = 0; visited[u] = 1; Q.push(u); dist[u] = 0; int v; int len; while( !Q.empty() ) { u = Q.front(); if(dist[u] > maxdist) { maxdist = dist[u]; farnode1 = u; } len = SZ(graph[u]); loop(i, len) { v = graph[u][i]; if(!visited[v]) { visited[v] = 1; dist[v] = dist[u] + cost[u][v]; Q.push(v); } } Q.pop(); } return maxdist; } int main() { //read(); int p, q, cst; string str; stringstream ss; while( ! cin.eof() ) { getline(cin, str); if(str == "") { pf("0\n"); getline(cin, str); continue; } loop(i, MAXX) graph[i].clear(); while( !cin.eof() && SZ(str) > 0 ) { if(str == "") break; ss.clear(); ss<<str; ss>>p>>q>>cst; graph[p].pb(q); graph[q].pb(p); cost[p][q] = cst; cost[q][p] = cst; getline(cin, str); } bfs(1); pf("%lld\n", bfs(farnode1)); } return 0; }