Posts

Showing posts from March, 2017

UVa 572 - Oil Deposits

/* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ // Accepted // Algorithm: DFS (Horizontal , Vertical, Diagonal) #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef ma

Ligthoj 1238 - Power Puff Girls

===================== Best Solution =========================== /* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ // Accepted by One BFS #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<in

ZOJ Modular Inverse

// Naive #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) //int main() //{ //    fast; //    int test , a , m , i , ans = -1; //    cin >> test; //    while(test--) //    { //        ans = -1; // //        cin >> a >> m; // //        for(i=1; i<=1000; i++) //        { //            if((a * i-1)%m == 0) // a * x  = 1 (mod m) = a * x - 1 = 0 (mod m) //            { //                ans = i; //                break; //            } //        } // //        if(ans == -1) cout << "Not Exist" << "\n"; //        else cout << ans << "\n"; //    } // //    return 0; //} // O(lgM) // Accepted int EGCD(int a , int b , int *x , int *y) {     if(a == 0)     {         *x = 0;         *y = 1;         return b;     }     int x1 , y1;     int gcd = EGCD(b%a , a, &x1 , &y1);     *x = y1 - (b / a) * x1; //  from geeksforgeeks analysis part     *y = x1; // from geeksforgeeks ana

1012 - Guilty Prince

/* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ // Algorithm: BFS (2D) // Accepted #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typ

Lightoj 1094 - Farthest Nodes in a Tree

/* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ // Algorithm: DFS // Accepted #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typedef

Lightoj 1174 - Commandos

/* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ /* Run BFS from the Source and Destination besides store the distance for all nodes on two different array when you run BFS from Source and Destination. Pick the biggest time with Summation from your stored distances... :) You have to find the minimum time that's why you have to Visit from both Source and Destination and at last you have to pick the biggest time for the minimum ... */ // Algorithm: BFS // Accepted #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i&l

UVa 532 - Dungeon Master

/* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ // Accepted // Algorithm: BFS (3D) #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typ

UVa 10653 - Bombs! NO they are Mines!!

#include<cstdio> #include<queue> #include<cstring> #include<cmath> using namespace std; // Accepted #define sf scanf #define pf printf const int high = 2003; int r  , c , adj[high][high] , cost[high][high]; int dir_x[4] = {1 , -1 , 0 , 0} , dir_y[4] = {0 , 0 , 1 , -1}; // Consider it as 4 direction co-ordinate bool visited[high][high]; void BFS(int x , int y) {     queue<int>Q;     Q.push(x);     Q.push(y);     adj[x][y] = -1;     visited[x][y] = true;     cost[x][y] = 0;     while(!Q.empty())     {         int ux = Q.front();         Q.pop();         int uy = Q.front();         Q.pop();         for(int i=0; i<4; i++)         {            int vx = ux + dir_x[i];             int vy = uy + dir_y[i];             if((vx >= 0 && vx <= r) && (vy >= 0 && vy <= c) && (adj[vx][vy] == 0))             {                 if(!visited[vx][vy] && !cost[vx][vy])                 {                     visited[vx][vy] =

UVa 11518 - Dominos 2

/* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ // Accepted #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typedef map<char, int&g

LightOJ 1166 - Old Sorting

//I am struggling //Just Not Good at it #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef map<int, int>mpii; const int mod = 1000007; const int high = 105; int ar[high] , dp[high][high] , br[high]; //int rec(int i, int j) //{ //    if(i >= j) return 0; // //    if(dp[i][j] >= 0) return dp[i][j]; // //    if(ar[i] == ar[j]) dp[i][j] = rec(i+1, j-1); //    else return dp[i][j] = 1+min(rec(i+1, j) , rec(i, j-1)); //    return dp[i][j]; //} int main() {     fast;     mpii pre, pres;     int t , tc=0 , n , i, preindex , presindex;     cin >> t;     while(t--)     {         pre.clear();         pres.clear();         cin

Codechef College And Gift

/* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typedef map<char, int>mpci; type

UVa 280 - Vertex

/* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ // Accepted #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define mset(c,v) memset(c , v , sizeof c) #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typedef map<char, int&g

UVa 10004 - Bicoloring

// Accepted #include<bits/stdc++.h> using namespace std; const int high = 500; vector<int>adj[high]; int color[high]; bool bicolorable = true; void adj_clear() {     for(int i=0; i<high; i++) adj[i].clear(); } void BFS(int s) {     queue<int>Q;     color[s] = 0;     Q.push(s);     while(!Q.empty())     {         int u = Q.front();         Q.pop();         for(int i=0; i<adj[u].size(); i++)         {             int v = adj[u][i];             if(!color[v])             {                 Q.push(v);                 color[v] = 1 + color[u]; // Just increase one from its parent node to make difference between two nodes.             }             else if(color[u] == color[v])             {                 bicolorable = false;                 break;             }         }         if(!bicolorable) break;     } } int main() {     //freopen("in.txt" , "r" , stdin);     ios_base::sync_with_stdio(false);     int i , n , l , u , v;     while(cin >>

UVa 924 - Spreading The News

Thanks to http://acmph.blogspot.com/2010/10/924-spreading-news-uva.html /* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ // Accepted #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii;

LightOJ 1009 - Back to Underworld

// Accepted /* Analysis: Consider a Graph as 1-2, 2-3, 2-4; if 1 is a vampire than 2 is lykan, as 2 is lykan so 3 and 4 will be vampire. Now total vampires are 3 and lykan is 1, so maximum from this graph is 3. [1=3=4=vampire] another graph is 5-6 and 6-7, 6-8. consider 5 is a vampire so 6 will be lykan. as 6 is lykan than the all adjacent of 6 will be vampire, i mean, 7=8=vampire. Now total vampires are 3 and lykan is 1, so maximum from this graph is 3. And the Answer of this total Graph is 3 + 3 = 6; [As, Graphs are not connected] Decision: If a node is Vampire than all of the adjacent of that node will be Lykan and if a node is Lykan than all of the adjacent of that node will be Vampire, thats why i have represented them as two color black and red. Black means Vampire and Red means Lykan, besides every time i have increased number of vampire and lykans and finally add the maximum as graphs are not connected. */ #include<cstdio> #include<cmath> #include<vector> #in

Lightoj 1111 - Best Picnic Ever

/* Lionel Messi is such a player that you may catch him, you may touch him, you may feel him and definitely you may Love him. Lionel Messi is Messi. A little Magician in this World. */ // Accepted #include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0) #define bfast cin.tie(0) #define outs(x) cout << x << " " #define outn(x) cout << x << "\n" #define sf scanf #define pf printf #define nl puts("") #define psb push_back #define loop0(n) for(int i=0; i<n; i++) #define loop1(n) for(int i=1; i<=n; i++) #define mpair(x , y) make_pair(x , y) #define all(x) x.begin(), x.end() #define pi acos(-1.0) #define psb push_back typedef unsigned long long ull; typedef long long LL; typedef vector<int>vii; typedef vector<LL>vll; typedef vector<string>vs; typedef map<int, int>mpii; typedef map<string, int>mpsi; typedef map<char, int>mpci; typedef map<LL, LL>mpll; type