├── .DS_Store ├── .Rhistory ├── Codejam ├── Codejam 2010 1A A.cpp ├── Codejam 2010 1A C.cpp └── Codejam 2010 1B C.cpp ├── Graph Algorithm Simulator ├── Autosave.gph ├── Euler.gph ├── Matrix_list ├── Optimalcolor.gph ├── README.md ├── Transform.h ├── Warshall_output ├── addcost.h ├── bellmanford.h ├── bipart.h ├── cgraph.h ├── clock.h ├── color.h ├── complement.h ├── complete.h ├── complete12.gph ├── credits.h ├── data_entry.h ├── details ├── dijikstrawiki.gph ├── display_control.h ├── drawing_text_tools.h ├── edge.h ├── euler2.gph ├── eulerP.h ├── eulerP.h.gch ├── filelist ├── func_bfs.h ├── func_dfs.h ├── func_dijikstra.h ├── func_mst.h ├── func_warshall.h ├── geometry.h ├── global_classes.h ├── infobar.h ├── io.h ├── keyboard.h ├── level.h ├── load_basic_background.h ├── macros ├── macros.h ├── main.cpp ├── mousemove.h ├── move.h ├── negcycle.gph ├── nodes.h ├── nodes.h.gch ├── optimalcolor.h ├── saveload.h ├── sidebar.h ├── sidebar2.h ├── sidebar2.h.gch ├── spsp.h ├── staticgraph.h ├── style.css ├── switches.h ├── temp ├── terminology.h ├── untitled.cpp ├── untitled.css └── wheel.h ├── LightOJ ├── 1240 - Point Segment Distance (3D) .cpp ├── 1095 - Arrange The Numbers.cpp ├── 1110 - An Easy LCS .cpp ├── 1157 - LCS Revisited.cpp ├── 1162 - Min Max Roads.cpp ├── 1266 - Points in Rectangle .cpp └── 1268 - Unlucky String.cpp ├── Others ├── APIO10-commandos.cpp ├── Andrew-Stankevich-10C-Order Preserving Codes.cpp ├── NorwayNCPC2012Galactic.cpp ├── NorwayNCPC2012Juice.cpp ├── Tree Generator.cpp ├── UCFLocalContest-Palindrome.cpp ├── USACO-MAR08-ACQUIRE.cpp └── ZOJ-2860(Breaking Strings).cpp ├── POJ ├── 2019 USACO-cornfields.cpp ├── 2182(USACO 2003 U S Open Orange-Lost Cows).cpp └── 3204- Ikki's Story I - Road Reconstruction.cpp ├── README.md ├── README.md~ ├── SPOJ ├── EXPRESS.cpp ├── Greed.cpp ├── Horrible.cpp ├── OTOCI.cpp ├── Paradox.py ├── QMAX3VN.cpp ├── SNAKYNUM.cpp ├── Sumsums.cpp └── Vonny.cpp ├── Template.cpp ├── Timus ├── 1052 Rabbit Hunt.cpp ├── 1069 Prufer Code.cpp ├── 1156.cpp ├── 1463.cpp ├── 1553 Caves And Tunnels.cpp ├── 1577.cpp ├── 1724.cpp ├── 1752.cpp └── 1861.cpp ├── Topcoder ├── BikeRiding.cpp ├── EllysBulls.cpp ├── MagicMolecule.cpp ├── NewArenaPassword.cpp └── PolygonTraversal.cpp ├── UVA ├── 10304(Optimal Binary Search Tree).cpp ├── 10354(Avoiding your boss).cpp ├── 10525(New to Bangladesh).cpp ├── 1062(Containers).cpp ├── 10746(Crime wave-sequel).cpp ├── 11031 - Looking for a Subset.cpp ├── 11119(Chemical Attraction).cpp ├── 11297(Census).cpp ├── 11419(Sam i am).cpp ├── 11918 - Traveler of Gridland.cpp ├── 12069 - Robots inside the Labyrinth.cpp ├── 12092 - Paint the Roads.cpp ├── 1220(Party at halibula).cpp ├── 12319(Edgetown's Traffic jam).cpp ├── 12424(Answering queries on a tree).cpp ├── 12607 Amazing Maze.cpp ├── 1310(One way traffic).cpp ├── 1345(Jamie's Contact Groups).cpp ├── 1400(Ray, Pass me the dishes!).cpp ├── 1424(Salesman).cpp ├── 318(Domino effect).cpp └── 685(Its not a bug).cpp ├── Useful C++ Libraries ├── Binary_indexed_tree.cpp ├── Matrix Exponentiation.cpp ├── generate_divisors_from_prime_factors.cpp └── rabin-carp.cpp ├── Uvalive ├── 4332-Blocks for kids(amritapuri 2008).cpp ├── 4333-Paper Presentation(amritapuri 2008).cpp ├── 4735- Not So Flat After All.cpp ├── 5070 Awkward Lights.cpp ├── 5070.cpp ├── UVALIVE 4538 - Here's a Product Which Will Make You Tensor.cpp └── UVALIVE 4540 Windows.cpp └── clean.sh /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shafaet/Programming-Contest-Algorithms/42ffba423186976481c9c2f2852e58aa75294750/.DS_Store -------------------------------------------------------------------------------- /.Rhistory: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shafaet/Programming-Contest-Algorithms/42ffba423186976481c9c2f2852e58aa75294750/.Rhistory -------------------------------------------------------------------------------- /Codejam/Codejam 2010 1A A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define stream istringstream 24 | #define rep(i,n) for(int i=0; i<(int)n; i++) 25 | #define repv(i,n) for(int i=n-1; i>=0; i--) 26 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 27 | #define replv(i,n) for(int i=n; i>=1; i--) 28 | 29 | #define SZ(x) (int)x.size() 30 | #define INF (1061109567) 31 | #define pb(x) push_back(x) 32 | #define ppb pop_back 33 | #define all(x) x.begin(),x.end() 34 | #define mem(x,y) memset(x,y,sizeof(x)); 35 | #define eps 1e-9 36 | #define pii pair 37 | #define pll pair 38 | #define pmp make_pair 39 | #define sdi(x) scanf("%d",&x) 40 | #define sdii(x,y) scanf("%d%d",&x,&y) 41 | #define sds(x) scanf("%s",x) 42 | #define pfi(x) printf("%d\n",x); 43 | #define uu first 44 | #define vv second 45 | #define pks printf("Case #%d: ",++ks); 46 | using namespace std; 47 | template inline T sqr(T x){return x*x;} 48 | double dist(double x1,double y1,double x2,double y2){return sqrt(sqr(x1-x2)+sqr(y1-y2));} 49 | template inline T lcm(T a,T b) {if(a<0)return 50 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 51 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 52 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 53 | typedef long long i64; 54 | typedef unsigned long long ui64; 55 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 56 | vector< string > token(char *a,char *b) {vectorv; char *p=strtok(a," "); while(p) {v.pb(p);p=strtok(NULL," ");} return v;} 57 | vectortoken(string a,char *b){char temp[100002]; strcpy(temp,a.c_str()); return token(temp,b); } 58 | 59 | 60 | //bool operator < ( const node& p ) const { return w < p.w; } 61 | #define READ(f) freopen(f, "r", stdin) 62 | #define WRITE(f) freopen(f, "w", stdout) 63 | int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; 64 | int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; 65 | 66 | 67 | #define on(n,pos) n=(n | (1<<(pos))) 68 | #define off(n,pos) n= n & ~(1<<(pos)) 69 | #define isOn(n,pos) (bool)(n & (1<ms; 117 | int st; 118 | for(st=0;st=n or tj<0 or tj>=n)break; 123 | ms.insert(w[ti][tj]); 124 | } 125 | if(st!=k) continue; 126 | if(SZ(ms)==1 and w[i][j]!='.') 127 | { 128 | if(w[i][j]=='R') red=1; 129 | if(w[i][j]=='B') blue=1; 130 | } 131 | } 132 | } 133 | } 134 | if(red and blue) return "Both"; 135 | if(red) return "Red"; 136 | if(blue)return "Blue"; 137 | return "Neither"; 138 | } 139 | int main() 140 | { 141 | READ("in"); 142 | //WRITE("Asm"); 143 | int t,ks=0; 144 | cin>>t; 145 | 146 | while(t--) 147 | { 148 | 149 | cin>>n>>k; 150 | rep(i,n) 151 | cin>>w[i]; 152 | 153 | rotate(); 154 | gravity(); 155 | pks; 156 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define stream istringstream 24 | #define rep(i,n) for(int i=0; i<(int)n; i++) 25 | #define repv(i,n) for(int i=n-1; i>=0; i--) 26 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 27 | #define replv(i,n) for(int i=n; i>=1; i--) 28 | 29 | #define SZ(x) (int)x.size() 30 | #define INF (1061109567) 31 | #define pb(x) push_back(x) 32 | #define ppb pop_back 33 | #define all(x) x.begin(),x.end() 34 | #define mem(x,y) memset(x,y,sizeof(x)); 35 | #define eps 1e-9 36 | #define pii pair 37 | #define pll pair 38 | #define pmp make_pair 39 | #define sdi(x) scanf("%d",&x) 40 | #define sdii(x,y) scanf("%d%d",&x,&y) 41 | #define sds(x) scanf("%s",x) 42 | #define pfi(x) printf("%d\n",x); 43 | #define uu first 44 | #define vv second 45 | #define pks printf("Case #%d: ",++ks); 46 | using namespace std; 47 | template inline T sqr(T x){return x*x;} 48 | double dist(double x1,double y1,double x2,double y2){return sqrt(sqr(x1-x2)+sqr(y1-y2));} 49 | template inline T lcm(T a,T b) {if(a<0)return 50 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 51 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 52 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 53 | typedef long long i64; 54 | typedef unsigned long long ui64; 55 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 56 | vector< string > token(char *a,char *b) {vectorv; char *p=strtok(a," "); while(p) {v.pb(p);p=strtok(NULL," ");} return v;} 57 | vectortoken(string a,char *b){char temp[100002]; strcpy(temp,a.c_str()); return token(temp,b); } 58 | 59 | 60 | //bool operator < ( const node& p ) const { return w < p.w; } 61 | #define READ(f) freopen(f, "r", stdin) 62 | #define WRITE(f) freopen(f, "w", stdout) 63 | int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; 64 | int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; 65 | 66 | 67 | #define on(n,pos) n=(n | (1<<(pos))) 68 | #define off(n,pos) n= n & ~(1<<(pos)) 69 | #define isOn(n,pos) (bool)(n & (1<=a1 and x<=a2) c+=a2-x; 82 | else if(x>t; 92 | while(t--) 93 | { 94 | int a1,a2,b1,b2; 95 | cin>>a1>>a2>>b1>>b2; 96 | pks; 97 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define stream istringstream 24 | #define rep(i,n) for(int i=0; i<(int)n; i++) 25 | #define repv(i,n) for(int i=n-1; i>=0; i--) 26 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 27 | #define replv(i,n) for(int i=n; i>=1; i--) 28 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 29 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 30 | #define SZ(x) (int)x.size() 31 | #define INF (1061109567) 32 | #define pb(x) push_back(x) 33 | #define ppb pop_back 34 | #define all(x) x.begin(),x.end() 35 | #define mem(x,y) memset(x,y,sizeof(x)); 36 | #define eps 1e-9 37 | #define pii pair 38 | #define pll pair 39 | #define pmp make_pair 40 | #define sdi(x) scanf("%d",&x) 41 | #define sdii(x,y) scanf("%d%d",&x,&y) 42 | #define sds(x) scanf("%s",x) 43 | #define pfi(x) printf("%d\n",x); 44 | #define uu first 45 | #define vv second 46 | using namespace std; 47 | template inline T sqr(T x){return x*x;} 48 | double dist(double x1,double y1,double x2,double y2){return sqrt(sqr(x1-x2)+sqr(y1-y2));} 49 | template inline T lcm(T a,T b) {if(a<0)return 50 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 51 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 52 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 53 | typedef long long i64; 54 | typedef unsigned long long ui64; 55 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 56 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 57 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 58 | b.end(), *q ) ) q++;}return oot; 59 | } 60 | #define on(n,pos) n=(n | (1<>t; 103 | mem(dp1,-1); 104 | mem(dp2,-1); 105 | while(t--) 106 | { 107 | i64 n,ans=0; 108 | cin>>n; 109 | repl(i,n-1) 110 | ans+=solve(n,i)%mod; 111 | pks; 112 | cout<> B C D 10 | B >> A C F 11 | C >> A B D F 12 | D >> A C E 13 | E >> D F 14 | F >> B C E 15 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/Optimalcolor.gph: -------------------------------------------------------------------------------- 1 | 16 2 | 145 126 1 _ 3 | 353 128 1 _ 4 | 534 133 1 _ 5 | 534 241 1 _ 6 | 375 251 1 _ 7 | 157 258 1 _ 8 | 605 402 1 _ 9 | 469 486 1 _ 10 | 625 500 1 _ 11 | 120 457 1 _ 12 | 297 550 1 _ 13 | 779 287 1 _ 14 | 545 573 1 _ 15 | 522 643 1 _ 16 | 419 613 1 _ 17 | 417 562 1 _ 18 | 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 19 | 1 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 20 | 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 21 | 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 22 | 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 23 | 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 24 | 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 25 | 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 26 | 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 27 | 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 28 | 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 29 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 | 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 31 | 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 32 | 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 33 | 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 34 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/README.md: -------------------------------------------------------------------------------- 1 | 2 | Copyright (C) <2010-11> 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | 17 | 18 | 19 | 20 | **Graph Algorithm Simulator** 21 | ------------------------------- 22 | 23 | **Features** 24 | ------------------------------- 25 | Draw graphs with straight lines using drawing tools 26 | Load graphs from file. Save graphs for using later 27 | Autosave the graph after every change 28 | Move the nodes in your desired position 29 | Color the nodes,change level as you like 30 | Make your graph a wheel ,a ring or a bipartite graph automatically 31 | Make complement graph,complete graph in a single click 32 | Simulate common graph algorithms. See the next slide for algorithm related info 33 | Update cost of the edges 34 | Delete node or edges 35 | 36 | 37 | 38 | **Supported Algorithms** 39 | ------------------------------- 40 | 41 | Kruskal’s minimum spanning tree: 42 | 43 | Simulate kruskal’s algorithm for finding mst and see how it works 44 | 45 | Depth First Search: 46 | 47 | “Cgraph Simulator” will simulate the dfs with colored line and will show you what actually happens when the algorithm runs. 48 | 49 | Breadth First Search: 50 | 51 | Run bfs and find single source shortest path(SSSP). Our simulator will show you exactly how it works 52 | 53 | Dijkstra: 54 | 55 | If the graph is weighted BFS won’t work. Simulate dijikstra to find SSSP. 56 | 57 | Floyed warshall: 58 | 59 | Our simulator will use floyed warshall to find apsp. As it is a complicated algorithm It wont visualize the simulation. 60 | 61 | Bellman ford: 62 | 63 | Find Negative weight cycle using this. This algorithm has resemblance to dijkstra. 64 | 65 | Optimal Graph Coloring 66 | 67 | Color the nodes in optimal way using backtrack and see how it works 68 | Finding Euler Circuit and path using backtrack 69 | 70 | Use backtrack to find euler circuit and path 71 | 72 | Limitation: 73 | 74 | The Simulator doesnt Support Directed graph yet. 75 | 76 | 77 | **How to Run** 78 | ----------------------- 79 | 80 | **Linux** 81 | Install needed packages: 82 | 83 | Ubuntu: 84 | sudo apt-get install g++ geany 85 | sudo apt-get install freeglut3 freeglut3-dev 86 | 87 | Fedora: 88 | yum install g++ freeglut-devel geany 89 | 90 | Now compile main.cpp using following parameters: 91 | -lglut -lGL -lGLU 92 | 93 | **Windows** 94 | Sorry, not supported yet. 95 | 96 | Done! 97 | 98 | 99 | **Author** 100 | ----------------------- 101 | Shafaet Ashraf 102 | 103 | Student, 104 | 105 | Computer Science and Engineering, University of Dhaka, 106 | 107 | Bangladesh 108 | 109 | This software was written as 1st year academic project in 2010. 110 | 111 | 112 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/Warshall_output: -------------------------------------------------------------------------------- 1 | Floyd Warshall Allpair shortest path Output: 2 | 0 7 9 10 14 8 3 | 7 0 10 11 7 1 4 | 9 10 0 1 10 11 5 | 10 11 1 0 9 12 6 | 14 7 10 9 0 6 7 | 8 1 11 12 6 0 8 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/addcost.h: -------------------------------------------------------------------------------- 1 | void getcost() 2 | { 3 | COLOR_RED; 4 | color_selected_node(select_2); 5 | 6 | freeze=1; 7 | 8 | COLOR_WHITE; 9 | class_SHAPE.FILLED_RECT((WIDTH/2)-40,(HEIGHT/2)-18,(WIDTH/2)+40,(HEIGHT/2)+18); 10 | if(signal_KEY) 11 | { 12 | 13 | if(CHAR_PRESSED==ENTER_KEY) 14 | { 15 | 16 | char buffer[10]; 17 | adj_matrix[select_1][select_2]=atoi(strcpy(buffer,input_cost.c_str())); 18 | adj_matrix[select_2][select_1]=atoi(strcpy(buffer,input_cost.c_str())); 19 | printf("%d\n",atoi(strcpy(buffer,input_cost.c_str()))); 20 | input_cost.clear(); 21 | freeze=0; 22 | RESET_SELECTION; 23 | mode_addcost=1; 24 | glutPostRedisplay(); 25 | } 26 | if(CHAR_PRESSED==BACKSPACE_KEY && input_cost.size()) 27 | { 28 | string::iterator it=input_cost.end()-1; 29 | input_cost.erase(it); 30 | 31 | } 32 | if( ((CHAR_PRESSED>='0' && CHAR_PRESSED<='9') || CHAR_PRESSED=='-') && input_cost.size()<=4) 33 | { 34 | 35 | input_cost.push_back(CHAR_PRESSED); 36 | } 37 | 38 | COLOR_RED; 39 | class_SHAPE.TEXT((WIDTH/2)-25,(HEIGHT/2),input_cost,font_medium); 40 | signal_KEY=0; 41 | } 42 | 43 | } 44 | void collect_cost_info() 45 | { 46 | 47 | COLOR_YELLOW; 48 | class_SHAPE.TEXT(130,10,"Select Pair of Edges to add costs",font_small); 49 | 50 | if(signal_LMB) 51 | { 52 | 53 | RESET_signal_MOUSE; 54 | if(!freeze) 55 | { 56 | if(!select_1) 57 | select_1=class_INFIGURE.Circle(struct_MOUSEAT.x,struct_MOUSEAT.y); 58 | else 59 | { 60 | select_2=class_INFIGURE.Circle(struct_MOUSEAT.x,struct_MOUSEAT.y); 61 | if(!select_2) 62 | select_1=0; 63 | 64 | } 65 | } 66 | 67 | } 68 | COLOR_BLUE; 69 | if(select_2) 70 | { 71 | if(adj_matrix[select_1][select_2]) 72 | getcost(); 73 | else { RESET_SELECTION;} 74 | } 75 | COLOR_RED; 76 | color_selected_node(select_1); 77 | } 78 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/bellmanford.h: -------------------------------------------------------------------------------- 1 | struct bell_edge{ 2 | int x, y,cost; 3 | 4 | }eb[100]; 5 | 6 | void run_bellman() 7 | { 8 | Negative_cycle=0; 9 | if(!Total_nodes) 10 | { 11 | WARNING_TEXT("WARNING::NO GRAPH DETECTED"); 12 | SWAPBUFFER 13 | class_TIMER.wait(2); 14 | mode_bell=0; 15 | } 16 | int value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_bipart_buttons); 17 | (value==2)?COLOR_BLUE:COLOR_RED; 18 | class_SHAPE.FILLED_RECT(WIDTH-160,HEIGHT-20,WIDTH-40,HEIGHT); 19 | COLOR_WHITE; 20 | class_SHAPE.TEXT(WIDTH-157,HEIGHT-13,"With Animation(slower)",font_small); 21 | (value==1)?COLOR_BLUE:COLOR_RED; 22 | class_SHAPE.FILLED_RECT(WIDTH-270,HEIGHT-20,WIDTH-190,HEIGHT); 23 | COLOR_WHITE; 24 | class_SHAPE.TEXT(WIDTH-267,HEIGHT-13,"No Animation",font_small); 25 | if(signal_LMB) 26 | { 27 | int value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_bipart_buttons); 28 | double speed=.4; 29 | if(value==1) speed=0; 30 | 31 | if(value) 32 | { 33 | int d[100]; 34 | REP(i,Total_nodes) 35 | { 36 | int tt; 37 | if(i==1) tt=0; 38 | else tt=inf; 39 | d[i] = tt; 40 | if(value==2) 41 | { 42 | COLOR_GREEN; 43 | write_dist(tt,i); 44 | } 45 | upd_bell[i]=0; 46 | } 47 | 48 | 49 | int cc=0; 50 | REP(i,Total_nodes) 51 | for(int j=i+1;j<=Total_nodes;j++) 52 | { 53 | if(adj_matrix[i][j]!=0) 54 | { 55 | cc++; 56 | eb[cc].x=i; 57 | eb[cc].y=j; 58 | eb[cc].cost=adj_matrix[i][j]; 59 | 60 | } 61 | 62 | } 63 | int i,j; 64 | 65 | for (i = 1; i ::iterator it; 18 | 19 | load_bipart_buttons(); 20 | for(it=list_bipart_nodes.begin();it!=list_bipart_nodes.end();it++) 21 | { 22 | COLOR_RED; 23 | color_selected_node(*it); 24 | } 25 | 26 | if(signal_LMB) 27 | { 28 | 29 | int value=class_INFIGURE.Circle(struct_MOUSEAT.x,struct_MOUSEAT.y); 30 | if(value) 31 | { 32 | int flag=1; 33 | for(it=list_bipart_nodes.begin();it!=list_bipart_nodes.end();it++) 34 | if(*it==value) flag=0; 35 | 36 | if(flag) 37 | list_bipart_nodes.push_back(value); 38 | else 39 | {WARNING_TEXT("Warning:: Already Selected"); } 40 | } 41 | value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_bipart_buttons); 42 | 43 | if(value) 44 | { 45 | if(!list_bipart_nodes.size()) {WARNING_TEXT("Error:: No Node Selected");} 46 | else 47 | { 48 | if(value==2)class_Transform.align_nodes(list_bipart_nodes); 49 | class_Transform.bipartite_graph(list_bipart_nodes); 50 | mode_bipart=0; 51 | } 52 | } 53 | 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/cgraph.h: -------------------------------------------------------------------------------- 1 | #include "clock.h" 2 | #include "switches.h" 3 | #include "global_classes.h" 4 | #include "display_control.h" 5 | #include "macros.h" 6 | #include "drawing_text_tools.h" 7 | #include "load_basic_background.h" 8 | #include "geometry.h" 9 | #include "color.h" 10 | #include "level.h" 11 | #include "terminology.h" 12 | #include "func_warshall.h" 13 | #include "staticgraph.h" 14 | #include "Transform.h" 15 | #include "io.h" 16 | #include "func_bfs.h" 17 | #include "func_dijikstra.h" 18 | #include "infobar.h" 19 | #include "nodes.h" 20 | #include "edge.h" 21 | #include "func_dfs.h" 22 | #include "func_mst.h" 23 | #include "sidebar.h" 24 | #include "sidebar2.h" 25 | #include "mousemove.h" 26 | #include "data_entry.h" 27 | #include "move.h" 28 | #include "addcost.h" 29 | #include "keyboard.h" 30 | #include "spsp.h" 31 | #include "complement.h" 32 | #include "credits.h" 33 | #include "optimalcolor.h" 34 | #include "bipart.h" 35 | #include "wheel.h" 36 | #include "bellmanford.h" 37 | #include "eulerP.h" 38 | 39 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/clock.h: -------------------------------------------------------------------------------- 1 | class MYCLOCK 2 | { 3 | public: 4 | void wait ( double seconds ) 5 | { 6 | 7 | 8 | clock_t endwait; 9 | endwait = clock () + seconds * CLOCKS_PER_SEC ; 10 | while (clock() < endwait) {} 11 | } 12 | 13 | void animator_line () 14 | { 15 | clock_t endwait; 16 | endwait = clock () + .035 * CLOCKS_PER_SEC ; 17 | while (clock() < endwait) {} 18 | } 19 | }; 20 | MYCLOCK class_TIMER; 21 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/color.h: -------------------------------------------------------------------------------- 1 | 2 | void COLOR_CHOICE(int n) 3 | { 4 | 5 | if(n==2) COLOR_RED; 6 | else if(n==3) COLOR_YELLOW; 7 | else if(n==4) COLOR_BLUE; 8 | else if(n==5) COLOR_GREEN; 9 | else if(n==6) COLOR_BROWN; 10 | else if(n==7) COLOR_DBLUE; 11 | else if(n==8) COLOR_PINK; 12 | else if(n==9) COLOR_ORANGE1; 13 | else if(n==10) COLOR_BROWN2; 14 | else if(n==11) COLOR_PINK2; 15 | else if(n==12) COLOR_MORE1; 16 | else if(n==13) COLOR_MORE2; 17 | else if(n==14) COLOR_MORE3; 18 | else COLOR_WHITE; 19 | 20 | } 21 | 22 | void color() 23 | { 24 | 25 | COLOR_YELLOW; 26 | class_SHAPE.TEXT(60,10,"Select a Node to Color it",font_small); 27 | 28 | 29 | 30 | if(signal_LMB) 31 | { 32 | RESET_signal_MOUSE; 33 | if(!select_for_color) 34 | { 35 | select_for_color=class_INFIGURE.Circle(struct_MOUSEAT.x,struct_MOUSEAT.y); 36 | 37 | } 38 | else 39 | { 40 | int value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_colorbox_buttons); 41 | if(value) 42 | { 43 | 44 | map_color[select_for_color]=value; 45 | select_for_color=0; 46 | } 47 | 48 | } 49 | 50 | } 51 | 52 | if(select_for_color) 53 | { 54 | glLineWidth(1); 55 | COLOR_BROWN; 56 | class_SHAPE.FILLED_RECT(WIDTH,0,WIDTH-125,HEIGHT); 57 | 58 | int value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_colorbox_buttons); 59 | int godown=0; 60 | for(int i=1;i<=7;i++) 61 | { 62 | COLOR_CHOICE(i); 63 | class_SHAPE.FILLED_RECT(WIDTH-110,HEIGHT-60-godown,WIDTH-10,HEIGHT-20-godown); 64 | if(i==value){glLineWidth(2);COLOR_BLACK;class_SHAPE.RECT(WIDTH-110,HEIGHT-60-godown,WIDTH-10,HEIGHT-20-godown);} 65 | godown+=50; 66 | 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/complement.h: -------------------------------------------------------------------------------- 1 | void complement_graph() 2 | { 3 | int copy_mat[100][100]; 4 | REP(i,Total_nodes) 5 | REP(j,Total_nodes) 6 | { 7 | copy_mat[i][j]=adj_matrix[i][j]; 8 | adj_matrix[i][j]=0; 9 | } 10 | 11 | REP(i,Total_nodes) 12 | { 13 | list_edges[i].clear(); 14 | } 15 | REP(i,Total_nodes) 16 | REP(j,Total_nodes) 17 | { 18 | if(i==j) continue; 19 | if(!copy_mat[i][j]) 20 | { 21 | adj_matrix[i][j]=1; 22 | list_edges[i].push_back(j); 23 | } 24 | 25 | } 26 | mode_complement=0; 27 | glutPostRedisplay(); 28 | } 29 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/complete.h: -------------------------------------------------------------------------------- 1 | class Transform 2 | { 3 | public: 4 | void complete_graph() 5 | { 6 | int flag=0; 7 | 8 | REP(i,Total_nodes) 9 | { 10 | for(int j=i+1;j<=Total_nodes;j++) 11 | { 12 | if(adj_matrix[i][j]) continue; 13 | adj_matrix[i][j]=1; 14 | adj_matrix[j][i]=1; 15 | list_edges[i].push_back(j); 16 | list_edges[j].push_back(i); 17 | int x1=map_coordinate[i].first; 18 | int y1=map_coordinate[i].second; 19 | flag=1; 20 | int x2=map_coordinate[j].first; 21 | int y2=map_coordinate[j].second; 22 | COLOR_BLUE; 23 | class_SHAPE.animate_LINE(x1,y1,x2,y2); 24 | } 25 | } 26 | if(!flag) 27 | { 28 | COLOR_RED; 29 | class_SHAPE.TEXT(60,35,"Warning!!: The Graph is already Complete",font_small); 30 | glutSwapBuffers(); 31 | class_TIMER.wait(1.5); 32 | } 33 | mode_complete=0; 34 | } 35 | 36 | }; 37 | Transform class_Transform; 38 | 39 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/complete12.gph: -------------------------------------------------------------------------------- 1 | 12 2 | 277 172 0 _ 3 | 426 202 0 _ 4 | 567 238 0 _ 5 | 565 360 0 _ 6 | 552 425 0 _ 7 | 443 476 0 _ 8 | 384 504 0 _ 9 | 277 522 0 _ 10 | 187 498 0 _ 11 | 165 439 0 _ 12 | 164 314 0 _ 13 | 162 190 0 _ 14 | 0 1 1 1 1 1 1 1 1 1 1 1 15 | 1 0 1 1 1 1 1 1 1 1 1 1 16 | 1 1 0 1 1 1 1 1 1 1 1 1 17 | 1 1 1 0 1 1 1 1 1 1 1 1 18 | 1 1 1 1 0 1 1 1 1 1 1 1 19 | 1 1 1 1 1 0 1 1 1 1 1 1 20 | 1 1 1 1 1 1 0 1 1 1 1 1 21 | 1 1 1 1 1 1 1 0 1 1 1 1 22 | 1 1 1 1 1 1 1 1 0 1 1 1 23 | 1 1 1 1 1 1 1 1 1 0 1 1 24 | 1 1 1 1 1 1 1 1 1 1 0 1 25 | 1 1 1 1 1 1 1 1 1 1 1 0 26 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/credits.h: -------------------------------------------------------------------------------- 1 | void check_close() 2 | { 3 | int value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_close_button); 4 | value?COLOR_YELLOW:COLOR_BLUE; 5 | class_SHAPE.FILLED_RECT(WIDTH,HEIGHT,WIDTH-60,HEIGHT-20); 6 | COLOR_RED; class_SHAPE.TEXT(WIDTH-45,HEIGHT-13,"Close",font_small); 7 | 8 | if(signal_LMB) 9 | { 10 | value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_close_button); 11 | if(value) mode_credits=0; 12 | } 13 | } 14 | void show_credits() 15 | { 16 | 17 | COLOR_RED; 18 | 19 | 20 | class_SHAPE.FILLED_RECT(0,0,WIDTH,HEIGHT); 21 | 22 | check_close(); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/data_entry.h: -------------------------------------------------------------------------------- 1 | void load_data(void) 2 | { 3 | SQBUTTONS temp; 4 | 5 | temp.downX=22; temp.downY=60-15; temp.upX=110; temp.upY=100-15; temp.id=sidebar_edge_button; 6 | list_sidebar_buttons.push_back(temp); 7 | temp.downX=22; temp.downY=120-15; temp.upX=110; temp.upY=160-15; temp.id=sidebar_vertex_button; 8 | list_sidebar_buttons.push_back(temp); 9 | temp.downX=22; temp.downY=180-15; temp.upX=110; temp.upY=220-15; temp.id=sidebar_movenode_button; 10 | list_sidebar_buttons.push_back(temp); 11 | temp.downX=22; temp.downY=180+60-15; temp.upX=110; temp.upY=220+60-15; temp.id=sidebar_addcost_button; 12 | list_sidebar_buttons.push_back(temp); 13 | temp.downX=22; temp.downY=180+60+60-15; temp.upX=110; temp.upY=220+60+60-15; temp.id=sidebar_write_button; 14 | list_sidebar_buttons.push_back(temp); 15 | temp.downX=22; temp.downY=180+60+60+60-15; temp.upX=110; temp.upY=220+60+60+60-15; temp.id=sidebar_clear_button; 16 | list_sidebar_buttons.push_back(temp); 17 | temp.downX=22; temp.downY=180+60+60+60+60-15; temp.upX=110; temp.upY=220+60+60+60+60-15; temp.id=sidebar_read_button; 18 | list_sidebar_buttons.push_back(temp); 19 | temp.downX=22; temp.downY=180+60+60+60+60+60-15; temp.upX=110; temp.upY=220+60+60+60+60+60-15; temp.id=sidebar_color_button; 20 | list_sidebar_buttons.push_back(temp); 21 | temp.downX=22; temp.downY=180+60+60+60+60+60+60-15; temp.upX=110; temp.upY=220+60+60+60+60+60+60-15; temp.id=sidebar_level_button; 22 | list_sidebar_buttons.push_back(temp); 23 | int goup=0,godown=0; 24 | REP(i,9) 25 | { 26 | temp.downX=const_margin_sidebar2+10; temp.downY=80+goup; temp.upX=const_margin_sidebar2+135; temp.upY=120+goup; 27 | temp.id=i; 28 | list_sidebar2_buttons.push_back(temp); 29 | goup+=60; 30 | } 31 | goup=0; 32 | REP(i,9) 33 | { 34 | 35 | temp.downX=const_margin_sidebar2+165; temp.downY=80+goup; temp.upX=const_margin_sidebar2+290; temp.upY=120+goup; 36 | temp.id=i+9; 37 | list_sidebar2_buttons.push_back(temp); 38 | goup+=60; 39 | } 40 | temp.downX=WIDTH-50; temp.downY=HEIGHT-20; temp.upX=WIDTH; temp.upY=HEIGHT; temp.id=button_Close; 41 | list_close_button.push_back(temp); 42 | temp.downX=WIDTH-140; temp.downY=HEIGHT-20; temp.upX=WIDTH-100; temp.upY=HEIGHT; temp.id=button_Copy; 43 | list_close_button.push_back(temp); 44 | temp.downX=WIDTH-400; temp.downY=HEIGHT-20; temp.upX=WIDTH-290; temp.upY=HEIGHT; temp.id=button_Remove; 45 | list_close_button.push_back(temp); 46 | 47 | 48 | temp.downX=WIDTH-60; temp.downY=0; temp.upX=WIDTH; temp.upY=20; temp.id=1; 49 | list_credits_button.push_back(temp); 50 | godown=0; 51 | REP(i,5) 52 | { 53 | 54 | temp.downX=WIDTH-110; 55 | temp.downY=HEIGHT-60-godown; 56 | temp.upX=WIDTH-10; 57 | temp.upY=HEIGHT-20-godown; 58 | temp.id=i; 59 | list_colorbox_buttons.push_back(temp); 60 | godown+=50; 61 | 62 | 63 | } 64 | 65 | temp.downX=WIDTH-160; temp.downY=HEIGHT-20; temp.upX=WIDTH-40; temp.upY=HEIGHT; temp.id=2; 66 | list_bipart_buttons.push_back(temp); 67 | temp.downX=WIDTH-270; temp.downY=HEIGHT-20; temp.upX=WIDTH-190; temp.upY=HEIGHT; temp.id=1; 68 | list_bipart_buttons.push_back(temp); 69 | 70 | } 71 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/details: -------------------------------------------------------------------------------- 1 | Negative Cycle Exists!! 2 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/dijikstrawiki.gph: -------------------------------------------------------------------------------- 1 | 6 2 | 109 161 3 A 3 | 588 78 3 B 4 | 435 363 3 C 5 | 167 554 3 D 6 | 542 602 3 E 7 | 726 380 3 F 8 | 0 7 9 14 0 0 9 | 7 0 10 0 0 1 10 | 9 10 0 1 0 11 11 | 14 0 1 0 9 0 12 | 0 0 0 9 0 6 13 | 0 1 11 0 6 0 14 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/display_control.h: -------------------------------------------------------------------------------- 1 | int WIDTH = 900; 2 | int HEIGHT = 700; 3 | int width = WIDTH; 4 | int height = HEIGHT; 5 | void reshapeMainWindow (int newWidth, int newHeight) 6 | { 7 | glMatrixMode( GL_PROJECTION ); 8 | glLoadIdentity(); 9 | glOrtho( 0, newWidth, 0, newHeight, 0, 1 ); 10 | glViewport( 0, 0, newWidth, newHeight ); 11 | WIDTH = newWidth; 12 | HEIGHT = newHeight; 13 | glMatrixMode(GL_MODELVIEW); 14 | glutPostRedisplay(); 15 | } 16 | 17 | int const const_margin_sidebar = 130; 18 | int const const_margin_sidebar2 = WIDTH-300; 19 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/drawing_text_tools.h: -------------------------------------------------------------------------------- 1 | class shapes 2 | { 3 | public: 4 | void LINE(int x1,int y1,int x2,int y2) 5 | { 6 | glBegin(GL_LINE_STRIP); 7 | glVertex2f(x1,y1); 8 | glVertex2f(x2,y2); 9 | glEnd(); 10 | 11 | } 12 | 13 | void FILLEDCIRCLE(int H,int K,int r) 14 | { 15 | glBegin(GL_LINE_LOOP); 16 | for(int rr=1;rry1) py+=threshold; 65 | else py-=threshold; 66 | glBegin(GL_LINE_STRIP); 67 | glVertex2f(x1+eps,y1+eps); 68 | glVertex2f(px+eps,py+eps); 69 | glEnd(); 70 | glutSwapBuffers(); 71 | class_TIMER.animator_line(); 72 | if(y2y1){if(py>y2 || fabs(py-y2)<1e-11) break;} 74 | } 75 | 76 | } 77 | else 78 | { 79 | while(1) 80 | { 81 | 82 | py=(px*(y1-y2)+(x1*y2)-(x2*y1))/(x1-x2); 83 | if(x2x1) px+=threshold; 85 | glBegin(GL_LINE_STRIP); 86 | glVertex2f(x1,y1); 87 | glVertex2f(px,py); 88 | glEnd(); 89 | glutSwapBuffers(); 90 | 91 | class_TIMER.animator_line(); 92 | if(x2x1){if(px>x2 || fabs(px-x2)<1e-11) break;} 94 | } 95 | } 96 | 97 | } 98 | }; 99 | 100 | shapes class_SHAPE; 101 | 102 | 103 | void WARNING_TEXT(string str) 104 | { 105 | 106 | COLOR_RED;class_SHAPE.TEXT(60,35,str,font_small); glutSwapBuffers();class_TIMER.wait(1); 107 | } 108 | void MASSAGE_TEXT(string str){ COLOR_YELLOW; class_SHAPE.TEXT(60,10,str,font_small);} 109 | void MASSAGE_TEXT_BIG(string str){ COLOR_YELLOW; class_SHAPE.TEXT(60,10,str,font_large);} 110 | 111 | void MASSAGE_TEXT_BIG2(string str){ COLOR_GREEN; class_SHAPE.TEXT(60,35,str,font_large);} 112 | 113 | void REMOVE_MASSAGE_TEXT_BIG(string str){ COLOR_BLACK; class_SHAPE.TEXT(60,10,str,font_large);} 114 | void REMOVE_MASSAGE_TEXT_BIG2(string str){ COLOR_BLACK; class_SHAPE.TEXT(60,35,str,font_large);} 115 | 116 | void REMOVE_MASSAGE_TEXT(string str){ COLOR_BLACK; class_SHAPE.TEXT(60,10,str,font_small);} 117 | void INFO_TEXT(string str){ COLOR_GREEN;class_SHAPE.TEXT(60,35,str,font_small); glutSwapBuffers();class_TIMER.wait(.5);} 118 | void REMOVE_WARNING_INFO_TEXT(string str) 119 | { 120 | 121 | COLOR_BLACK;class_SHAPE.TEXT(60,35,str,font_small); glutSwapBuffers();class_TIMER.wait(1); 122 | } 123 | void color_selected_node(int node) 124 | { 125 | if(!node)return; 126 | map >::iterator mit; 127 | int node_num=1; 128 | 129 | for(mit=map_coordinate.begin();mit!=map_coordinate.end();mit++) 130 | { 131 | if(node_num==node) 132 | { 133 | 134 | int H=(*mit).second.first; 135 | int K=(*mit).second.second; 136 | class_SHAPE.FILLEDCIRCLE(H,K,default_radius); 137 | break; 138 | } 139 | node_num++; 140 | } 141 | 142 | 143 | } 144 | 145 | string fprint_level(int n) 146 | { 147 | char lv[20]; 148 | if(map_level[n]=="" ||map_level[n]=="_") 149 | { 150 | sprintf(lv,"%d",n); 151 | return lv; 152 | 153 | } 154 | return map_level[n]; 155 | } 156 | string fprint_dist(int d) 157 | { 158 | char temp[10]; 159 | if(d>=INF) return "infinity"; 160 | sprintf(temp,"%d",d); 161 | return temp; 162 | 163 | } 164 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/edge.h: -------------------------------------------------------------------------------- 1 | void collect_edge_info() 2 | { 3 | int value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_bipart_buttons); 4 | (value==2 || !mode_delete)?COLOR_GREEN:COLOR_BROWN; 5 | class_SHAPE.FILLED_RECT(WIDTH-160,HEIGHT-20,WIDTH-40,HEIGHT); 6 | COLOR_WHITE; class_SHAPE.TEXT(WIDTH-157,HEIGHT-13,"Add Edge",font_small); 7 | (value==1 || mode_delete)?COLOR_GREEN:COLOR_BROWN; 8 | class_SHAPE.FILLED_RECT(WIDTH-270,HEIGHT-20,WIDTH-190,HEIGHT); 9 | COLOR_WHITE;class_SHAPE.TEXT(WIDTH-267,HEIGHT-13,"Delete Edge",font_small); 10 | 11 | 12 | if(signal_LMB) 13 | { 14 | value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_bipart_buttons); 15 | if(value==1) mode_delete=1; if(value==2) mode_delete=0; 16 | 17 | if(!select_1) 18 | { 19 | select_1=class_INFIGURE.Circle(struct_MOUSEAT.x,struct_MOUSEAT.y); 20 | COLOR_YELLOW; 21 | MASSAGE_TEXT("Select Pair of Edges to Connect or delete them"); 22 | 23 | } 24 | else 25 | { 26 | select_2=class_INFIGURE.Circle(struct_MOUSEAT.x,struct_MOUSEAT.y); 27 | if(select_2) 28 | { 29 | if(mode_delete && adj_matrix[select_1][select_2]) 30 | { 31 | adj_matrix[select_1][select_2]=0; 32 | adj_matrix[select_2][select_1]=0; 33 | list::iterator it; 34 | for(it=list_edges[select_1].begin();it!=list_edges[select_1].end();it++)if((*it)==select_2) {list_edges[select_1].erase(it); break;} 35 | for(it=list_edges[select_2].begin();it!=list_edges[select_2].end();it++)if((*it)==select_1) {list_edges[select_2].erase(it); break;} 36 | createfile_and_writegraph("Autosave"); 37 | } 38 | 39 | else if(!adj_matrix[select_1][select_2] && !adj_matrix[select_2][select_1] && select_2!=select_1) 40 | { 41 | list_edges[select_1].push_back(select_2); 42 | list_edges[select_2].push_back(select_1); 43 | adj_matrix[select_1][select_2]=1; 44 | adj_matrix[select_2][select_1]=1; 45 | createfile_and_writegraph("Autosave"); 46 | 47 | } 48 | 49 | else {COLOR_RED;class_SHAPE.TEXT(60,35,"Warning: Already connected!",font_small); glutSwapBuffers();class_TIMER.wait(1); } 50 | } 51 | select_2=0; 52 | select_1=0; 53 | } 54 | } 55 | COLOR_RED; 56 | color_selected_node(select_1); 57 | } 58 | 59 | 60 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/euler2.gph: -------------------------------------------------------------------------------- 1 | 8 2 | 212 169 0 A 3 | 483 147 0 B 4 | 645 291 0 C 5 | 405 483 0 D 6 | 209 511 0 E 7 | 306 111 0 F 8 | 427 608 0 G 9 | 291 607 0 H 10 | 0 1 0 1 1 1 0 0 11 | 1 0 1 1 0 1 0 0 12 | 0 1 0 1 0 0 0 0 13 | 1 1 1 0 1 0 1 1 14 | 1 0 0 1 0 0 0 0 15 | 1 1 0 0 0 0 0 0 16 | 0 0 0 1 0 0 0 1 17 | 0 0 0 1 0 0 1 0 18 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/eulerP.h: -------------------------------------------------------------------------------- 1 | int odd_deg_count() 2 | { 3 | int odd=0; 4 | MASSAGE_TEXT_BIG("Counting vertexes with odd degree"); 5 | SWAPBUFFER;class_TIMER.wait(.3); 6 | REP(i,Total_nodes) 7 | { 8 | int cnt=0; 9 | REP(j,Total_nodes) 10 | { 11 | if(adj_matrix[i][j]) cnt++; 12 | 13 | } 14 | if(cnt%2==1){ odd++; COLOR_GREEN; color_selected_node(i);SWAPBUFFER;class_TIMER.wait(.5);} 15 | } 16 | REMOVE_MASSAGE_TEXT_BIG("Counting vertexes with odd degree"); 17 | SWAPBUFFER;class_TIMER.wait(.3); 18 | return odd; 19 | 20 | 21 | 22 | } 23 | void eulerp_backtrack(int src,int vis[][50],int T_edge, FILE*fp,int speed) 24 | { 25 | if((int)vect_eulerP.size()==T_edge+1) 26 | { 27 | vector::iterator mit; 28 | int goright=0,godown=0;; 29 | for(mit=vect_eulerP.begin();mit=650) {goright=0; godown=25;} 40 | } 41 | if(vect_eulerP[0]==vect_eulerP.back())fprintf(fp,"[+Circuit]"); 42 | REP(i,Total_nodes) REP(j,Total_nodes)if(adj_matrix[i][j]) { if(speed==SLOWER){COLOR_RED;connect(i,j,1);}} 43 | 44 | if(speed==SLOWER) 45 | { 46 | SWAPBUFFER; 47 | class_TIMER.wait(.12); 48 | } 49 | 50 | LOAD_BACKGROUND(); 51 | class_static_graph.Load_edges(); 52 | class_static_graph.Load_nodes(); 53 | 54 | REP(i,Total_nodes) REP(j,Total_nodes)if(adj_matrix[i][j]) {if(speed==SLOWER){COLOR_GREEN;connect(i,j,1);}} 55 | if(speed==SLOWER){SWAPBUFFER; class_TIMER.wait(.4);} 56 | fprintf(fp,"\n"); 57 | 58 | 59 | } 60 | list::iterator it; 61 | for(it=list_edges[src].begin();it!=list_edges[src].end();it++) 62 | { 63 | if(!vis[src][*it] && !vis[*it][src]) 64 | { 65 | 66 | vis[src][*it]=1; vis[*it][src]=1; 67 | if(speed==SLOWER){COLOR_GREEN; 68 | connect(src,*it,1);} 69 | if(speed==SLOWER){SWAPBUFFER; class_TIMER.wait(.5);} 70 | vect_eulerP.pb(*it); 71 | eulerp_backtrack(*it,vis,T_edge,fp,speed); 72 | 73 | vect_eulerP.ppb(); 74 | vis[src][*it]=0; vis[*it][src]=0; 75 | if(speed==SLOWER){COLOR_BLUE; 76 | connect(src,*it,1);} 77 | if(speed==SLOWER){SWAPBUFFER; class_TIMER.wait(.2);} 78 | 79 | } 80 | 81 | } 82 | 83 | 84 | } 85 | 86 | void run_eulerP() 87 | { 88 | 89 | int value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_bipart_buttons); 90 | (value==2)?COLOR_BLUE:COLOR_RED; 91 | class_SHAPE.FILLED_RECT(WIDTH-160,HEIGHT-20,WIDTH-40,HEIGHT); 92 | COLOR_WHITE; 93 | class_SHAPE.TEXT(WIDTH-157,HEIGHT-13,"Show Animaton",font_small); 94 | (value==1)?COLOR_BLUE:COLOR_RED; 95 | class_SHAPE.FILLED_RECT(WIDTH-270,HEIGHT-20,WIDTH-190,HEIGHT); 96 | COLOR_WHITE; 97 | class_SHAPE.TEXT(WIDTH-267,HEIGHT-13,"No Animation",font_small); 98 | if(signal_LMB && value) 99 | { 100 | WARNING_TEXT("WARNING::The backtrack algorithm may take long time if the graph is slightly large"); 101 | SWAPBUFFER; class_TIMER.wait(1); 102 | REMOVE_WARNING_INFO_TEXT("WARNING::The backtrack algorithm may take long time if the graph is slightly large"); 103 | int vis[50][50]; 104 | 105 | 106 | FILE *fp=fopen("details","w"); 107 | fprintf(fp,"Euler Paths: \n"); 108 | int T_odd=odd_deg_count(); 109 | if(T_odd==2 || T_odd==0) 110 | { 111 | REP(k,Total_nodes) 112 | { 113 | vect_eulerP.clear(); 114 | int T_edge=0; 115 | REP(i,Total_nodes) 116 | REP(j,Total_nodes) 117 | { 118 | if(adj_matrix[i][j])T_edge++; 119 | vis[i][j]=0; 120 | } 121 | T_edge/=2; 122 | vect_eulerP.pb(k); 123 | eulerp_backtrack(k,vis,T_edge,fp,value); 124 | 125 | } 126 | } 127 | else 128 | { 129 | fprintf(fp,"Graph with euler path must have exactly 0 or 2 odd degree,This graph doesnt fill the condition: \n"); 130 | MASSAGE_TEXT_BIG("The graph must have exactly 0 or 2 odd degree to have euler path"); 131 | SWAPBUFFER; class_TIMER.wait(1.2); 132 | REMOVE_MASSAGE_TEXT_BIG("The graph must have exactly 0 or 2 odd degree to have euler path"); 133 | } 134 | fclose(fp); 135 | switch_infobar=1; 136 | //mode_eulerP=0; 137 | } 138 | 139 | } 140 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/eulerP.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shafaet/Programming-Contest-Algorithms/42ffba423186976481c9c2f2852e58aa75294750/Graph Algorithm Simulator/eulerP.h.gch -------------------------------------------------------------------------------- /Graph Algorithm Simulator/filelist: -------------------------------------------------------------------------------- 1 | addcost.h 2 | Autosave.gph 3 | bellmanford.h 4 | bipart.h 5 | cgraph.h 6 | clock.h 7 | color.h 8 | complement.h 9 | complete12.gph 10 | complete.h 11 | credits.h 12 | data_entry.h 13 | details 14 | dijikstrawiki.gph 15 | display_control.h 16 | drawing_text_tools.h 17 | edge.h 18 | euler2.gph 19 | Euler.gph 20 | eulerP.h 21 | eulerP.h.gch 22 | filelist 23 | func_bfs.h 24 | func_dfs.h 25 | func_dijikstra.h 26 | func_mst.h 27 | func_warshall.h 28 | geometry.h 29 | global_classes.h 30 | infobar.h 31 | io.h 32 | keyboard.h 33 | level.h 34 | load_basic_background.h 35 | macros 36 | macros.h 37 | main 38 | main.cpp 39 | main.o 40 | Matrix_list 41 | mousemove.h 42 | move.h 43 | negcycle.gph 44 | nodes.h 45 | nodes.h.gch 46 | Optimalcolor.gph 47 | optimalcolor.h 48 | saveload.h 49 | sidebar2.h 50 | sidebar2.h.gch 51 | sidebar.h 52 | spsp.h 53 | staticgraph.h 54 | style.css 55 | switches.h 56 | temp 57 | terminology.h 58 | Transform.h 59 | untitled.cpp 60 | untitled.css 61 | Warshall_output 62 | wheel.h 63 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/func_bfs.h: -------------------------------------------------------------------------------- 1 | void write_detail_bfs() 2 | { 3 | FILE *fp=fopen("details","w"); 4 | 5 | fprintf(fp,"BFS OUTPUT:\n\nSOURCE NODE : %s\n",fprint_level(source_sssp).c_str()); 6 | REP(i,Total_nodes) 7 | { 8 | fprintf(fp,"Dist from Node %s to Node %s is %s\n",fprint_level(source_sssp).c_str(),fprint_level(i).c_str(),fprint_dist(sssp_dis[i]).c_str()); 9 | } 10 | 11 | fclose(fp); 12 | } 13 | void bfs_sssp(int source,int *cmap,int *d,int speed) 14 | { 15 | 16 | queueactQ; 17 | queuewaitQ; 18 | actQ.push(source); 19 | cmap[source]=1; 20 | int level=1; 21 | while(true) 22 | { 23 | while(!actQ.empty()) 24 | { 25 | int top=actQ.front(); actQ.pop(); 26 | list::iterator it; 27 | for(it=list_edges[top].begin(); it!=list_edges[top].end();it++) 28 | { 29 | 30 | if(!cmap[*it]) 31 | { 32 | d[*it]=level; 33 | 34 | int x1=map_coordinate[*it].first; 35 | int y1=map_coordinate[*it].second; 36 | COLOR_ORANGE; 37 | class_SHAPE.FILLEDCIRCLE(x1,y1,default_radius); 38 | int tx=map_coordinate[top].first; 39 | int ty=map_coordinate[top].second; 40 | COLOR_GREEN; 41 | class_SHAPE.FILLEDCIRCLE(tx,ty,default_radius); 42 | COLOR_RED; 43 | glLineWidth(2); 44 | if(speed==FASTER)class_SHAPE.LINE(tx,ty,x1,y1); 45 | else class_SHAPE.animate_LINE(tx,ty,x1,y1); 46 | write_dist(level,*it); 47 | glutSwapBuffers(); 48 | class_TIMER.wait(.5); 49 | 50 | cmap[*it]=1; 51 | waitQ.push(*it); 52 | 53 | } 54 | 55 | } 56 | 57 | 58 | } 59 | level++; 60 | if(waitQ.empty()) break; 61 | while(!waitQ.empty()) 62 | { 63 | actQ.push(waitQ.front()); 64 | waitQ.pop(); 65 | 66 | } 67 | 68 | } 69 | glLineWidth(1); 70 | COLOR_BLACK; 71 | 72 | 73 | 74 | COLOR_BLUE; 75 | 76 | glutSwapBuffers(); 77 | class_TIMER.wait(1); 78 | } 79 | 80 | 81 | 82 | int select_node() 83 | { 84 | //LOAD_BACKGROUND(); 85 | //class_static_graph.Load_nodes(); 86 | //class_static_graph.Load_edges(); 87 | // glutSwapBuffers(); 88 | 89 | if(signal_LMB) 90 | { 91 | int value=class_INFIGURE.Circle(struct_MOUSEAT.x,struct_MOUSEAT.y); 92 | return value; 93 | 94 | } 95 | 96 | 97 | return 0; 98 | 99 | } 100 | 101 | 102 | void run_sssp() 103 | { 104 | if(!source_sssp) 105 | { 106 | source_sssp=select_node(); 107 | MASSAGE_TEXT_BIG("Please Select a node"); 108 | } 109 | 110 | else 111 | { 112 | COLOR_RED; color_selected_node(source_sssp); 113 | int value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_bipart_buttons); 114 | (value==2)?COLOR_BLUE:COLOR_RED; 115 | class_SHAPE.FILLED_RECT(WIDTH-160,HEIGHT-20,WIDTH-40,HEIGHT); 116 | COLOR_WHITE; 117 | class_SHAPE.TEXT(WIDTH-157,HEIGHT-13,"Slower Animation",font_small); 118 | (value==1)?COLOR_BLUE:COLOR_RED; 119 | class_SHAPE.FILLED_RECT(WIDTH-270,HEIGHT-20,WIDTH-190,HEIGHT); 120 | COLOR_WHITE; 121 | class_SHAPE.TEXT(WIDTH-267,HEIGHT-13,"Faster Animation",font_small); 122 | 123 | if(signal_LMB && value) 124 | { 125 | 126 | 127 | 128 | REMOVE_MASSAGE_TEXT_BIG("Please Select a node"); 129 | for(int i=1;i<30;i++)sssp_dis[i]=inf; 130 | 131 | MASSAGE_TEXT("Simulating breadth-first-search alogrithm to find Single Source Shortest Path"); 132 | 133 | sssp_dis[source_sssp]=0; 134 | int cmap[100]={0}; 135 | bfs_sssp(source_sssp,cmap,sssp_dis,value); 136 | COLOR_BLACK; 137 | REMOVE_MASSAGE_TEXT("Simulating breadth-first-search algorithm to find Single Source Shortest Path"); 138 | write_detail_bfs(); 139 | switch_infobar=1; 140 | 141 | 142 | 143 | } 144 | } 145 | 146 | } 147 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/func_dijikstra.h: -------------------------------------------------------------------------------- 1 | void write_detail_dij() 2 | { 3 | FILE *fp=fopen("details","w"); 4 | fprintf(fp,"DIJKSTRA OUTPUT: SOURCE NODE : %s\n",fprint_level(source_sssp_dij).c_str()); 5 | REP(i,Total_nodes) 6 | { 7 | fprintf(fp,"Dist from Node %s to Node %s is %s\n",fprint_level(source_sssp_dij).c_str(),fprint_level(i).c_str(),fprint_dist(sssp_dis_dij[i]).c_str()); 8 | } 9 | 10 | fclose(fp); 11 | 12 | } 13 | 14 | void dijkstra(int source,int value) 15 | { 16 | if(value==SLOWER) 17 | MASSAGE_TEXT("Running DIJKSTRA to Find single source shortest path"); 18 | REP(i,Total_nodes) 19 | { 20 | 21 | if(value==SLOWER) 22 | { 23 | INFO_TEXT("Assigning Infinity to all nodes except source!"); 24 | COLOR_BLUE; 25 | write_dist(sssp_dis_dij[i],i); 26 | glutSwapBuffers(); class_TIMER.wait(.2); 27 | } 28 | if(i==source) sssp_dis_dij[i]=0; 29 | else sssp_dis_dij[i]=inf; 30 | 31 | } 32 | REMOVE_WARNING_INFO_TEXT("Assigning Infinity to all nodes except source!"); 33 | priority_queue actq; 34 | priority_queue waitq; 35 | data e; 36 | e.city=source; 37 | e.dist=0; 38 | actq.push(e); 39 | while(true) 40 | { 41 | if(value==SLOWER)INFO_TEXT("Updating Distance!"); 42 | while(!actq.empty()) 43 | { 44 | 45 | data top=actq.top(); 46 | actq.pop(); 47 | int topcost=sssp_dis_dij[top.city]; 48 | list::iterator it; 49 | 50 | 51 | int H=map_coordinate[top.city].first; 52 | int K=map_coordinate[top.city].second; 53 | if(value==SLOWER) 54 | { 55 | COLOR_CHOICE(GREEN); 56 | 57 | class_SHAPE.FILLEDCIRCLE(H,K,default_radius); 58 | glutSwapBuffers(); class_TIMER.wait(.3); 59 | } 60 | 61 | 62 | for(it=list_edges[top.city].begin();it!=list_edges[top.city].end();it++) 63 | { 64 | 65 | data temp; 66 | temp.city=*it; 67 | temp.dist=adj_matrix[*it][top.city]+topcost; 68 | if(sssp_dis_dij[temp.city]>temp.dist) 69 | { 70 | 71 | 72 | if(value==SLOWER) 73 | { 74 | int H2=map_coordinate[temp.city].first; 75 | int K2=map_coordinate[temp.city].second; 76 | COLOR_BLACK; write_dist(sssp_dis_dij[temp.city],temp.city); 77 | COLOR_RED; write_dist(temp.dist,temp.city); 78 | class_SHAPE.LINE(H,K,H2,K2); 79 | COLOR_CHOICE(YELLOW); 80 | class_SHAPE.FILLEDCIRCLE(H2,K2,default_radius); 81 | glutSwapBuffers(); class_TIMER.wait(.4); 82 | COLOR_BLUE; 83 | class_SHAPE.LINE(H,K,H2,K2); 84 | } 85 | 86 | 87 | sssp_dis_dij[temp.city]=temp.dist; 88 | waitq.push( temp ); 89 | } 90 | } 91 | if(value==SLOWER) 92 | { 93 | COLOR_CHOICE(RED); 94 | class_SHAPE.FILLEDCIRCLE(H,K,default_radius); 95 | glutSwapBuffers(); 96 | class_TIMER.wait(.3); 97 | } 98 | 99 | 100 | } 101 | if(waitq.empty()) break; 102 | 103 | if(value==SLOWER){glutSwapBuffers(); class_TIMER.wait(.3);} 104 | while(!waitq.empty()) 105 | { 106 | 107 | actq.push(waitq.top()); 108 | waitq.pop(); 109 | } 110 | 111 | } 112 | REMOVE_WARNING_INFO_TEXT("Updating Distance!"); 113 | write_detail_dij(); 114 | 115 | } 116 | 117 | 118 | void run_sssp_dij() 119 | { 120 | 121 | if(!source_sssp_dij) 122 | { 123 | MASSAGE_TEXT_BIG("Please Select a node"); 124 | 125 | source_sssp_dij=select_node(); //defined in func_bfs 126 | } 127 | else 128 | { 129 | COLOR_RED; color_selected_node(source_sssp_dij); 130 | int value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_bipart_buttons); 131 | (value==2)?COLOR_BLUE:COLOR_RED; 132 | class_SHAPE.FILLED_RECT(WIDTH-160,HEIGHT-20,WIDTH-40,HEIGHT); 133 | COLOR_WHITE; 134 | class_SHAPE.TEXT(WIDTH-157,HEIGHT-13,"With Animation",font_small); 135 | (value==1)?COLOR_BLUE:COLOR_RED; 136 | class_SHAPE.FILLED_RECT(WIDTH-270,HEIGHT-20,WIDTH-190,HEIGHT); 137 | COLOR_WHITE; 138 | class_SHAPE.TEXT(WIDTH-267,HEIGHT-13,"No Animation",font_small); 139 | if(signal_LMB && value) 140 | { 141 | dijkstra(source_sssp_dij,value); 142 | source_sssp=0; 143 | switch_infobar=1; 144 | } 145 | } 146 | 147 | } 148 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/func_mst.h: -------------------------------------------------------------------------------- 1 | void write_detail_mst() 2 | { 3 | FILE *fp=fopen("details","w"); 4 | fprintf(fp,"MST OUTPUT:\n\n\n"); 5 | fprintf(fp,"Spanning Cost: %d\n",spanning_cost); 6 | for(vector::iterator it=vect_mst.begin();it::iterator it; 28 | for(it=mst_min_vect.begin();it:: iterator it; 114 | for(it=list_edges[i].begin();it!=list_edges[i].end();it++) 115 | { 116 | mst_min temp; 117 | temp.u=i; temp.v=*it; temp.w=adj_matrix[i][*it]; 118 | if(!taken[temp.u][temp.v] && !taken[temp.v][temp.u]) 119 | { 120 | mst_min_vect.push_back(temp); 121 | taken[temp.u][temp.v]=1; taken[temp.v][temp.u]=1; 122 | } 123 | 124 | } 125 | 126 | } 127 | 128 | vector::iterator it; 129 | sort(mst_min_vect.begin(),mst_min_vect.end()); 130 | 131 | for(i=0;i<=1000;i++) pr[i]=i; 132 | spanning_cost=run_kruskals_mst_min(value); 133 | 134 | glLineWidth(1); 135 | write_detail_mst(); 136 | switch_infobar=1; 137 | } 138 | } 139 | 140 | } 141 | 142 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/func_warshall.h: -------------------------------------------------------------------------------- 1 | void write_warshall() 2 | { 3 | 4 | int mat[100][100]; 5 | REP(i,Total_nodes) 6 | REP(j,Total_nodes) 7 | { 8 | mat[i][j]=adj_matrix[i][j]; 9 | if(!mat[i][j] && i!=j) mat[i][j]=INF; 10 | 11 | } 12 | 13 | REP(k,Total_nodes) 14 | REP(i,Total_nodes) 15 | REP(j,Total_nodes) 16 | if (mat[i][j]> mat[i][k]+mat[k][j]) mat[i][j]= mat[i][k]+mat[k][j]; 17 | FILE *fp=fopen("Warshall_output","w"); 18 | fprintf(fp,"Floyd Warshall Allpair shortest path Output:\n"); 19 | REP(i,Total_nodes) 20 | { 21 | REP(j,Total_nodes) 22 | { 23 | fprintf(fp,"%d ",mat[i][j]); 24 | 25 | } 26 | fprintf(fp,"\n"); 27 | } 28 | fclose(fp); 29 | system("geany Warshall_output"); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/geometry.h: -------------------------------------------------------------------------------- 1 | class point_in_figures{ 2 | public: 3 | 4 | int RECT(int x,int y,listbuttons) 5 | { 6 | list::iterator it; 7 | for(it=buttons.begin();it!=buttons.end();it++) 8 | if(x>(*it).downX && x<(*it).upX && y>(*it).downY && y<(*it).upY) return (*it).id; 9 | 10 | return 0; 11 | } 12 | int Circle(int x,int y) 13 | { 14 | 15 | int temp=1; 16 | map >::iterator mit; 17 | 18 | for(mit=map_coordinate.begin();mit!=map_coordinate.end();mit++) 19 | { 20 | int H=(*mit).second.first; 21 | int K=(*mit).second.second; 22 | int dist=(x-H)*(x-H)+(y-K)*(y-K); 23 | if(dist<=default_radius*default_radius)return temp; 24 | temp++; 25 | 26 | } 27 | return 0; 28 | } 29 | }; 30 | 31 | point_in_figures class_INFIGURE; 32 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/global_classes.h: -------------------------------------------------------------------------------- 1 | class current_mouse 2 | { 3 | public: 4 | int x,y; 5 | 6 | }; 7 | current_mouse struct_MOUSEAT; 8 | 9 | struct SQBUTTONS 10 | { 11 | int upX,upY,downX,downY,id; 12 | 13 | }; 14 | 15 | listlist_sidebar_buttons; 16 | listlist_sidebar2_buttons; 17 | listlist_close_button; 18 | listlist_credits_button; 19 | listlist_files_button; 20 | listlist_colorbox_buttons; 21 | listlist_bipart_buttons; 22 | 23 | listlist_connected_components[30]; 24 | listlist_edges[30]; 25 | int adj_matrix[100][100]; 26 | map >map_coordinate; 27 | mapmap_level; 28 | vectorvect_path; 29 | 30 | struct mst_min 31 | { 32 | int u,v; 33 | int w; 34 | bool operator < ( const mst_min &b) const 35 | { 36 | return wmst_min_vect; 40 | priority_queuemstmin_Q; 41 | 42 | 43 | struct data { 44 | int city, dist; 45 | bool operator < ( const data& p ) const { 46 | return dist > p.dist; 47 | } 48 | }; 49 | mapmap_color; 50 | mapclr; 51 | 52 | listlist_bipart_nodes; 53 | 54 | void clear_all() 55 | { 56 | int i,j; 57 | map_coordinate.clear(); 58 | for(i=0;i<30;i++) 59 | { 60 | list_edges[i].clear(); 61 | list_connected_components[i].clear(); 62 | } 63 | map_color.clear(); 64 | map_level.clear(); 65 | Total_nodes=0; 66 | for(i=0;i<100;i++) for(j=0;j<100;j++) adj_matrix[i][j]=0; 67 | } 68 | 69 | struct edges_mst{int x,y;}; 70 | vectorvect_mst; 71 | vectorvect_eulerP; 72 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/infobar.h: -------------------------------------------------------------------------------- 1 | void justkeep_mst() 2 | { 3 | REP(i,Total_nodes) 4 | { 5 | REP(j,Total_nodes) 6 | adj_matrix[i][j]=0; 7 | 8 | list_edges[i].clear(); 9 | 10 | } 11 | vector::iterator it; 12 | for(it=vect_mst.begin();it::iterator it; 92 | for(it=vect_mst.begin();it bool inside(T a, T b, T c) { return a<=b && b<=c; } 7 | #define ON true 8 | #define OFF false 9 | #define font_small GLUT_BITMAP_HELVETICA_10 10 | #define font_large GLUT_BITMAP_HELVETICA_18 11 | #define font_medium GLUT_BITMAP_HELVETICA_12 12 | #define COLOR_RED glColor3f(255.0f, 0.0f, 0.0f) 13 | #define COLOR_BLACK glColor3f(0.0f, 0.0f, 0.0f) 14 | #define COLOR_WHITE glColor3f(1.0f, 1.0f, 1.0f) 15 | #define COLOR_YELLOW glColor3f(255.0f, 110.0f, 0.0f) 16 | #define COLOR_BLUE glColor3f(75.0/256.0, 175/256.0, 255/256.0) 17 | #define COLOR_GREEN glColor3f(0.0/256.0, 239/256.0, 7/256.0) 18 | #define COLOR_BROWN glColor3f(118.0/256.0, 34/256.0, 21/256.0) 19 | #define COLOR_BLUE glColor3f(75.0/256.0, 175/256.0, 255/256.0) 20 | #define COLOR_GREEN glColor3f(0.0/256.0, 239/256.0, 7/256.0) 21 | #define COLOR_BROWN glColor3f(118.0/256.0, 34/256.0, 21/256.0) 22 | #define COLOR_DBLUE glColor3f(35.0/256.0, 25/256.0, 87/256.0) 23 | #define COLOR_PINK glColor3f(235.0/256.0, 41/256.0, 173/256.0) 24 | #define COLOR_ORANGE1 glColor3f(220.0/256.0, 120/256.0, 8/256.0) 25 | #define COLOR_ORANGE glColor3f(255.0/256.0, 124/256.0, 0/256.0) 26 | #define COLOR_BROWN2 glColor3f(186.0/256.0, 156/256.0, 123/256.0) 27 | #define COLOR_PINK2 glColor3f(240.0/256.0, 195/256.0, 194/256.0) 28 | #define COLOR_MORE1 glColor3f(192.0/256.0, 171/256.0, 63/256.0) 29 | #define COLOR_MORE2 glColor3f(184.0/256.0, 191/256.0, 68/256.0) 30 | #define COLOR_MORE3 glColor3f(132.0/256.0, 40/256.0, 151/256.0) 31 | #define BLACK -5 32 | #define WHITE 1 33 | #define RED 2 34 | #define YELLOW 3 35 | #define BLUE 4 36 | #define GREEN 5 37 | #define BROWN 6 38 | #define DBLUE 7 39 | #define PINK 8 40 | #define ORANGE1 9 41 | #define BROWN2 10 42 | #define PINK2 11 43 | #define MORE1 12 44 | #define MORE2 13 45 | #define MORE3 14 46 | #define default_radius 7 47 | #define ENTER_KEY 13 48 | #define BACKSPACE_KEY 8 49 | #define VALID_KEYS (CHAR_PRESSED>='0' && CHAR_PRESSED<='9') || CHAR_PRESSED==ENTER_KEY || CHAR_PRESSED==BACKSPACE_KEY 50 | #define SWAPBUFFER glutSwapBuffers(); 51 | #define REDISPLAY glutPostRedisplay(); 52 | 53 | #define CONDITION_SIDEBAR2 struct_MOUSEAT.x>((switch_sidebar2==ON)? const_margin_sidebar2:WIDTH-15)&& !select_for_move && !select_for_color && !freeze 54 | #define CONDITION_SIDEBAR1 struct_MOUSEAT.x<((switch_sidebar==ON)? const_margin_sidebar:15) && !select_for_move && !select_for_color && !freeze 55 | 56 | #define VALID_LEVEL_KEY (inside('a',CHAR_PRESSED,'z') || inside('A',CHAR_PRESSED,'Z') || inside('0',CHAR_PRESSED,'9')) 57 | #define FASTER 1 58 | #define SLOWER 2 59 | #define pb push_back 60 | #define ppb pop_back 61 | #define VALID_AREA_FOR_NODE struct_MOUSEAT.y>48 && struct_MOUSEAT.y <670 && struct_MOUSEAT.x<870 && struct_MOUSEAT.x>30 62 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | #include "cgraph.h" 16 | 17 | 18 | void display () 19 | { 20 | 21 | LOAD_BACKGROUND(); 22 | class_static_graph.Load_edges(); 23 | class_static_graph.Load_nodes(); 24 | 25 | 26 | 27 | 28 | if(!switch_infobar && !mode_write && !mode_read && !mode_credits) 29 | { 30 | if(mode_complete) class_Transform.complete_graph(); 31 | if(mode_ring) class_Transform.ring_graph(); 32 | if(mode_complement) complement_graph(); 33 | if(mode_optimal) optimal_color(); 34 | if(mode_bipart) bipart_info(); 35 | if(mode_wheel) wheel_info(); 36 | if(mode_bell) run_bellman(); 37 | if(mode_eulerP) run_eulerP(); 38 | if(mode_minspanning_tree)collect_minspan_info(); 39 | if(mode_term)switch_infobar=1; 40 | if(CONDITION_SIDEBAR1) {switch_sidebar=ON;SHOW_SIDEBAR();} 41 | else switch_sidebar=OFF; 42 | if(switch_sidebar==ON) SIDEBAR_FUNC(); 43 | 44 | if(CONDITION_SIDEBAR2) {switch_sidebar2=ON;SHOW_SIDEBAR2();} 45 | else switch_sidebar2=OFF; 46 | if(switch_sidebar2==ON) SIDEBAR2_FUNC(); 47 | 48 | 49 | if(!mode_edge && !mode_addcost) RESET_SELECTION; 50 | if(mode_sssp) run_sssp(); 51 | else if(mode_sssp_dij) run_sssp_dij(); 52 | else if(mode_spsp) {find_spsp();} 53 | else if(mode_union_find)run_union_find(); 54 | else if(switch_sidebar==OFF && mode_vertex) 55 | collect_node_info(); 56 | else if(switch_sidebar==OFF && mode_edge ) 57 | collect_edge_info(); 58 | else if(switch_sidebar==OFF && mode_movenode ) 59 | move_the_nodes(); 60 | else if(switch_sidebar==OFF && mode_addcost) 61 | collect_cost_info(); 62 | else if(switch_sidebar==OFF && mode_color) 63 | { 64 | color(); 65 | } 66 | else if(switch_sidebar==OFF && mode_level) 67 | { 68 | level(); 69 | } 70 | } 71 | 72 | 73 | if(mode_credits) {show_credits();} 74 | if(switch_infobar) 75 | { 76 | if(mode_union_find) {info_dfs();} 77 | if(mode_sssp) {info_sssp();} 78 | if(mode_sssp_dij) {info_sssp_dij();} 79 | if(mode_minspanning_tree){info_mst();} 80 | if(mode_eulerP){info_euler();} 81 | if(mode_term){info_term();} 82 | if(mode_bell){info_bell();} 83 | } 84 | if(mode_write || mode_read)filebrowser(); 85 | 86 | 87 | 88 | 89 | 90 | glutSwapBuffers(); 91 | } 92 | 93 | 94 | int main(int argc, char *argv[]) 95 | { 96 | 97 | load_data(); 98 | glutInit(&argc, argv); 99 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 100 | glutInitWindowSize(width, height); 101 | glutCreateWindow("CGRAPH SIMULATOR"); 102 | glutDisplayFunc(display); 103 | glutKeyboardFunc(graphicKeys); 104 | glutPassiveMotionFunc(mouseMovement); 105 | glutMouseFunc(mouseAction); 106 | glutReshapeFunc(reshapeMainWindow); 107 | glutMainLoop(); 108 | return 0; 109 | } 110 | 111 | 112 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/mousemove.h: -------------------------------------------------------------------------------- 1 | void mouseAction(int button, int state, int x, int z) 2 | { 3 | if(freeze) return; 4 | 5 | if(button==GLUT_LEFT_BUTTON) 6 | { 7 | if (state == GLUT_DOWN) 8 | { 9 | signal_LMB=1; 10 | glutPostRedisplay(); 11 | } 12 | if(state==GLUT_UP) 13 | { 14 | 15 | RESET_signal_MOUSE 16 | glutPostRedisplay(); 17 | } 18 | } 19 | if(button==GLUT_RIGHT_BUTTON) 20 | { 21 | if (state == GLUT_DOWN) 22 | { 23 | signal_RMB=1; 24 | glutPostRedisplay(); 25 | } 26 | if(state==GLUT_UP) 27 | { 28 | RESET_signal_MOUSE 29 | //glutPostRedisplay(); 30 | } 31 | 32 | } 33 | } 34 | 35 | void mouseMovement(int mx, int my) 36 | { 37 | 38 | struct_MOUSEAT.x=mx; 39 | struct_MOUSEAT.y=HEIGHT-my; 40 | if(!freeze) 41 | glutPostRedisplay(); 42 | } 43 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/move.h: -------------------------------------------------------------------------------- 1 | void move_the_nodes() 2 | { 3 | 4 | int x,y; 5 | if(select_for_move) 6 | { 7 | 8 | if(struct_MOUSEAT.y>58 && struct_MOUSEAT.x>35) 9 | { 10 | map_coordinate[select_for_move].first=struct_MOUSEAT.x; 11 | map_coordinate[select_for_move].second=struct_MOUSEAT.y; 12 | int H=map_coordinate[select_for_move].first; 13 | int K=map_coordinate[select_for_move].second; 14 | COLOR_RED; 15 | class_SHAPE.FILLEDCIRCLE(H,K,default_radius); 16 | write_number(select_for_move,H,K); 17 | if(signal_LMB) 18 | select_for_move=0; 19 | } 20 | 21 | } 22 | else if(signal_LMB) 23 | { 24 | 25 | 26 | select_for_move=class_INFIGURE.Circle(struct_MOUSEAT.x,struct_MOUSEAT.y); 27 | if(select_for_move) 28 | { 29 | x=map_coordinate[select_for_move].first; 30 | y=map_coordinate[select_for_move].second; 31 | createfile_and_writegraph("Autosave"); 32 | } 33 | 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/negcycle.gph: -------------------------------------------------------------------------------- 1 | 15 2 | 248 132 0 _ 3 | 562 149 0 _ 4 | 711 204 0 _ 5 | 717 353 0 _ 6 | 622 473 0 _ 7 | 552 546 0 _ 8 | 214 493 0 _ 9 | 141 342 0 _ 10 | 355 214 0 _ 11 | 547 355 0 _ 12 | 457 466 0 _ 13 | 300 420 0 _ 14 | 303 324 0 _ 15 | 433 261 0 _ 16 | 459 331 0 _ 17 | 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 18 | 0 0 -10 0 0 0 0 0 0 1 0 0 0 0 0 19 | 0 -10 0 33 0 0 0 0 0 0 0 0 0 0 0 20 | 0 0 33 0 1 0 0 0 0 1 0 0 0 0 0 21 | 0 0 0 1 0 12 0 0 0 -10 0 0 0 0 0 22 | 0 0 0 0 12 0 0 0 0 0 1 0 0 0 0 23 | 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 24 | 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 25 | 1 0 0 0 0 0 0 0 0 0 0 0 -20 1 0 26 | 0 1 0 1 -10 0 0 0 0 0 0 0 0 0 0 27 | 0 0 0 0 0 1 0 0 0 0 0 -15 0 0 5 28 | 0 0 0 0 0 0 1 8 0 0 -15 0 2 0 0 29 | 0 0 0 0 0 0 0 0 -20 0 0 2 0 0 0 30 | 0 0 0 0 0 0 0 0 1 0 0 0 0 0 8 31 | 0 0 0 0 0 0 0 0 0 0 5 0 0 8 0 32 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/nodes.h: -------------------------------------------------------------------------------- 1 | 2 | int too_close(int atX,int atY) 3 | { 4 | map >::iterator mit; 5 | 6 | for(mit=map_coordinate.begin();mit!=map_coordinate.end();mit++) 7 | { 8 | 9 | int H=(*mit).second.first; 10 | int K=(*mit).second.second; 11 | if(H==atX && K==atY) continue; 12 | int dist=(atX-H)*(atX-H)+(atY-K)*(atY-K); 13 | 14 | if(dist<2*4*default_radius*default_radius)return 1; 15 | 16 | } 17 | return 0; 18 | } 19 | void collect_node_info() 20 | { 21 | COLOR_YELLOW; 22 | MASSAGE_TEXT("Click left button to Draw Nodes"); 23 | int value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_bipart_buttons); 24 | (value==1 || mode_delete)?COLOR_GREEN:COLOR_BROWN; 25 | class_SHAPE.FILLED_RECT(WIDTH-270,HEIGHT-20,WIDTH-187,HEIGHT); 26 | COLOR_WHITE;class_SHAPE.TEXT(WIDTH-267,HEIGHT-13,"Delete Last Node",font_small); 27 | if(signal_LMB) 28 | { 29 | value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_bipart_buttons); 30 | if(value) 31 | { 32 | list::iterator it; 33 | list_edges[Total_nodes].clear(); 34 | 35 | 36 | REP(i,Total_nodes-1) 37 | { 38 | int flag=0; 39 | while(1) 40 | { 41 | flag=0; 42 | for(it=list_edges[i].begin();it!=list_edges[i].end();it++) 43 | if(*it==Total_nodes) 44 | { 45 | flag=1; 46 | //printf(">>%d %d\n",i,*it); 47 | list_edges[i].erase(it); 48 | break; 49 | } 50 | if(!flag) break; 51 | } 52 | 53 | } 54 | REP(i,Total_nodes)REP(j,Total_nodes)if(i==Total_nodes || j==Total_nodes) adj_matrix[i][j]=0; 55 | map_coordinate.erase(Total_nodes); 56 | if(Total_nodes) 57 | Total_nodes--; 58 | 59 | 60 | } 61 | else 62 | { 63 | pairtemp_pair; 64 | 65 | if(!too_close(struct_MOUSEAT.x,struct_MOUSEAT.y) && VALID_AREA_FOR_NODE && Total_nodes<=29) 66 | { 67 | Total_nodes++; 68 | temp_pair.first=struct_MOUSEAT.x; temp_pair.second=struct_MOUSEAT.y; 69 | map_coordinate[Total_nodes]=temp_pair; 70 | 71 | createfile_and_writegraph("Autosave"); 72 | } 73 | else 74 | { 75 | 76 | COLOR_RED; 77 | if(Total_nodes==30) class_SHAPE.TEXT(60,35,"Warning: Cant draw more then 30 nodes",font_small); 78 | else 79 | { 80 | 81 | class_SHAPE.TEXT(60,35,"Warning!!: Cant draw in restricted areas or too close to another node",font_small); 82 | 83 | } 84 | glutSwapBuffers(); 85 | class_TIMER.wait(.8); 86 | } 87 | } 88 | } 89 | 90 | } 91 | 92 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/nodes.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shafaet/Programming-Contest-Algorithms/42ffba423186976481c9c2f2852e58aa75294750/Graph Algorithm Simulator/nodes.h.gch -------------------------------------------------------------------------------- /Graph Algorithm Simulator/optimalcolor.h: -------------------------------------------------------------------------------- 1 | void colorit(int src,int*vis) 2 | { 3 | list::iterator it; 4 | for(it=list_edges[src].begin();it!=list_edges[src].end();it++) 5 | { 6 | 7 | if(!vis[*it] && !clr[*it]) 8 | { 9 | list::iterator mit; 10 | int tmp[10]={0}; 11 | for(mit=list_edges[*it].begin();mit!=list_edges[*it].end();mit++) 12 | { 13 | if(*it==*mit)break; 14 | tmp[clr[*mit]]=1; 15 | 16 | } 17 | for(int i=2;i<=14;i++)if(!tmp[i]){clr[*it]=i; break;} 18 | 19 | vis[*it]=1; 20 | colorit(*it,vis); 21 | 22 | } 23 | 24 | } 25 | 26 | } 27 | 28 | void optimal_color() 29 | { 30 | clr.clear(); 31 | 32 | int vis[100]={0}; 33 | REP(i,Total_nodes) 34 | { 35 | if(!vis[i]) 36 | { 37 | clr[i]=2; 38 | vis[i]=1; 39 | colorit(i,vis); 40 | } 41 | } 42 | map_color=clr; 43 | mode_optimal=0; 44 | REDISPLAY 45 | } 46 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/saveload.h: -------------------------------------------------------------------------------- 1 | void save_graph() 2 | { 3 | FILE *fp=fopen("out","w"); 4 | REP(i,Total_nodes) 5 | { 6 | fprintf(fp,"%6d %6d %6d\n",i,map_coordinate[i].first,map_coordinate[i].second); 7 | } 8 | fprintf(fp,"\n\n"); 9 | REP(i,Total_nodes) 10 | { 11 | REP(j,Total_nodes) 12 | { 13 | fprintf(fp,"%d ",adj_matrix[i][j]); 14 | } 15 | fprintf(fp,"\n"); 16 | } 17 | fclose(fp); 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/sidebar.h: -------------------------------------------------------------------------------- 1 | 2 | void SIDEBAR_FUNC() 3 | { 4 | RESET_mouse_on 5 | int value=0; 6 | value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_sidebar_buttons); 7 | 8 | if(value==sidebar_edge_button) 9 | mouse_on_Edge_button=1; 10 | if(value==sidebar_vertex_button) mouse_on_Vertex_button=1; 11 | if(value==sidebar_movenode_button) mouse_on_movenode_button=1; 12 | if(value==sidebar_addcost_button) mouse_on_addcost_button=1; 13 | if(value==sidebar_write_button) mouse_on_write_button=1; 14 | if(value==sidebar_clear_button) mouse_on_clear_button=1; 15 | if(value==sidebar_read_button) mouse_on_read_button=1; 16 | if(value==sidebar_color_button) mouse_on_color_button=1; 17 | if(value==sidebar_level_button) mouse_on_level_button=1; 18 | 19 | if(signal_LMB) 20 | { 21 | value=0; 22 | value=class_INFIGURE.RECT(struct_MOUSEAT.x,struct_MOUSEAT.y,list_sidebar_buttons); 23 | if(value) RESET_BAR_MODES; 24 | if(value==sidebar_edge_button) mode_edge=1; 25 | if(value==sidebar_vertex_button) mode_vertex=1; 26 | if(value==sidebar_movenode_button) mode_movenode=1; 27 | if(value==sidebar_addcost_button) mode_addcost=1; 28 | if(value==sidebar_write_button) {switch_sidebar=OFF;mode_write=1; } 29 | if(value==sidebar_read_button) {switch_sidebar=OFF;enter_name_now=0;mode_read=1; } 30 | if(value==sidebar_color_button) {switch_sidebar=OFF;mode_color=1; } 31 | if(value==sidebar_level_button) {switch_sidebar=OFF;mode_level=1; } 32 | if(value==sidebar_clear_button) clear_all(); 33 | signal_LMB=0; 34 | } 35 | 36 | } 37 | 38 | 39 | void SHOW_SIDEBAR() 40 | { 41 | 42 | COLOR_RED; 43 | class_SHAPE.FILLED_RECT(0,0,const_margin_sidebar,HEIGHT); 44 | 45 | COLOR_BLACK; 46 | class_SHAPE.TEXT(30,HEIGHT-20,"CGRAPH",font_large); 47 | class_SHAPE.TEXT(10,HEIGHT-40,"SIMULATOR",font_large); 48 | 49 | (mouse_on_Edge_button || mode_edge)==true? COLOR_YELLOW:COLOR_WHITE; 50 | class_SHAPE.FILLED_RECT(22,60-15,110,100-15); //edge_button 51 | 52 | (mouse_on_Vertex_button || mode_vertex)==true? COLOR_YELLOW:COLOR_WHITE; 53 | class_SHAPE.FILLED_RECT(22,120-15,110,160-15); //vertex_button 54 | 55 | (mouse_on_movenode_button || mode_movenode)==true? COLOR_YELLOW:COLOR_WHITE; 56 | class_SHAPE.FILLED_RECT(22,180-15,110,220-15); //vertex_button 57 | 58 | (mouse_on_addcost_button || mode_addcost)==true? COLOR_YELLOW:COLOR_WHITE; 59 | class_SHAPE.FILLED_RECT(22,180+60-15,110,220+60-15); 60 | 61 | (mouse_on_write_button || mode_write)==true? COLOR_YELLOW:COLOR_WHITE; 62 | class_SHAPE.FILLED_RECT(22,180+60+60-15,110,220+60+60-15); 63 | (mouse_on_clear_button )==true? COLOR_YELLOW:COLOR_WHITE; 64 | class_SHAPE.FILLED_RECT(22,180+60+60+60-15,110,220+60+60+60-15); 65 | (mouse_on_read_button || mode_write)==true? COLOR_YELLOW:COLOR_WHITE; 66 | class_SHAPE.FILLED_RECT(22,180+60+60+60+60-15,110,220+60+60+60+60-15); 67 | (mouse_on_color_button || mode_color)==true? COLOR_YELLOW:COLOR_WHITE; 68 | class_SHAPE.FILLED_RECT(22,180+120+120+60-15,110,220+240+60-15); 69 | (mouse_on_level_button || mode_level)==true? COLOR_YELLOW:COLOR_WHITE; 70 | class_SHAPE.FILLED_RECT(22,180+120+120+60+60-15,110,220+240+60+60-15); 71 | 72 | COLOR_RED; 73 | class_SHAPE.TEXT(30,80-15,"Edge Mode",font_small); 74 | class_SHAPE.TEXT(30,140-15,"Vertex Mode",font_small); 75 | class_SHAPE.TEXT(30,200-15,"Move Node",font_small); 76 | class_SHAPE.TEXT(30,260-15,"Update Cost",font_small); 77 | class_SHAPE.TEXT(30,320-15,"Load Graph",font_small); 78 | class_SHAPE.TEXT(30,380-15,"Clear All",font_small); 79 | class_SHAPE.TEXT(30,440-15,"Save Graph",font_small); 80 | class_SHAPE.TEXT(30,500-15,"Node Color",font_small); 81 | class_SHAPE.TEXT(30,560-15,"Node Level",font_small); 82 | } 83 | 84 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/sidebar2.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shafaet/Programming-Contest-Algorithms/42ffba423186976481c9c2f2852e58aa75294750/Graph Algorithm Simulator/sidebar2.h.gch -------------------------------------------------------------------------------- /Graph Algorithm Simulator/spsp.h: -------------------------------------------------------------------------------- 1 | void path (int i, int j,int p[][100]) { 2 | if (i!=j) 3 | path(i,p[i][j],p); 4 | vect_path.push_back(j); 5 | 6 | } 7 | void RUN_WARSHALL(int src,int dest) 8 | { 9 | vect_path.clear(); 10 | int mat[100][100]; 11 | int p[100][100]; 12 | REP(i,Total_nodes) 13 | REP(j,Total_nodes) 14 | { 15 | mat[i][j]=adj_matrix[i][j]; 16 | if(!mat[i][j] && i!=j) mat[i][j]=INF; 17 | 18 | p[i][j]=i; 19 | } 20 | 21 | REP(k,Total_nodes) 22 | REP(i,Total_nodes) 23 | REP(j,Total_nodes) 24 | if (mat[i][j]> mat[i][k]+mat[k][j]) 25 | { 26 | mat[i][j]= mat[i][k]+mat[k][j]; 27 | p[i][j]= p[k][j]; 28 | } 29 | if(mat[src][dest] >::iterator mit; 64 | 65 | for(mit=map_coordinate.begin();mit!=map_coordinate.end();mit++) 66 | { 67 | int clr=map_color[node_num]; 68 | if(mode_sssp_dij || mode_union_find || mode_sssp)COLOR_WHITE; 69 | else 70 | COLOR_CHOICE(clr); 71 | 72 | int H=(*mit).second.first; 73 | int K=(*mit).second.second; 74 | if(select_for_move!=node_num) 75 | { 76 | 77 | class_SHAPE.FILLEDCIRCLE(H,K,default_radius); 78 | write_number((*mit).first,H,K); 79 | } 80 | if(sssp_static) 81 | { 82 | write_dist(sssp_dis[node_num],node_num); 83 | 84 | } 85 | node_num++; 86 | } 87 | } 88 | void Load_edges() 89 | { 90 | list::iterator it; 91 | if(mode_bipart || mode_wheel || mode_ring) 92 | { 93 | COLOR_BLACK; 94 | class_SHAPE.FILLED_RECT(WIDTH-15,HEIGHT,15,51); 95 | } 96 | 97 | for(int i=1;i<=Total_nodes;i++) 98 | { 99 | int x1=map_coordinate[i].first; 100 | int y1=map_coordinate[i].second; 101 | for(it=list_edges[i].begin();it!=list_edges[i].end();it++) 102 | { 103 | int x2=map_coordinate[*it].first; 104 | int y2=map_coordinate[*it].second; 105 | COLOR_BLUE; 106 | class_SHAPE.LINE(x1,y1,x2,y2); 107 | int ecost=adj_matrix[i][*it]; 108 | if(show_cost) 109 | write_cost(x1,y1,x2,y2,ecost); 110 | 111 | } 112 | 113 | } 114 | } 115 | 116 | }; 117 | static_graph class_static_graph; 118 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/switches.h: -------------------------------------------------------------------------------- 1 | bool switch_sidebar; 2 | bool switch_sidebar2; 3 | bool switch_infobar; 4 | 5 | int const sidebar_edge_button = 1; 6 | int const sidebar_vertex_button = 2; 7 | int const sidebar_movenode_button = 3; 8 | int const sidebar_addcost_button = 4; 9 | int const sidebar_write_button = 5; 10 | int const sidebar_clear_button = 6; 11 | int const sidebar_read_button = 7; 12 | int const sidebar_color_button = 8; 13 | int const sidebar_level_button = 9; 14 | 15 | int const sidebar2_unionfind_button = 1; 16 | int const sidebar2_sssp_button = 2; 17 | int const sidebar2_togglecost_button = 3; 18 | int const sidebar2_minspanning_tree_button = 4; 19 | int const sidebar2_sssp_dij_button = 5; 20 | int const sidebar2_bipart_button = 6; 21 | int const sidebar2_Eulerpath_button = 7; 22 | int const sidebar2_term_button = 8; 23 | 24 | int const sidebar2_spsp_button=10; 25 | int const sidebar2_complete_button=11; 26 | int const sidebar2_complement_button=12; 27 | int const sidebar2_optimal_button=13; 28 | int const sidebar2_ring_button=14; 29 | int const sidebar2_wheel_button=15; 30 | int const sidebar2_bell_button=16; 31 | int const sidebar2_matlist_button=17; 32 | int const sidebar2_warshall_button=18; 33 | 34 | int const button_Close=1; 35 | int const button_Copy=2; 36 | int const button_Remove=3; 37 | int const button_Credits=1; 38 | 39 | int const bipart_move=1; 40 | int const bipart_nomove=2; 41 | bool mode_vertex; 42 | bool mode_edge; 43 | bool mode_movenode; 44 | bool mode_addcost; 45 | bool mode_write; 46 | bool mode_read; 47 | bool mode_color; 48 | bool mode_level; 49 | bool mode_complement; 50 | bool mode_credits; 51 | 52 | 53 | 54 | #define RESET_BAR_MODES mode_vertex=0; mode_edge=0,mode_movenode=0,mode_addcost=0,mode_write=0,mode_color=0,mode_level=0,mode_credits=0; 55 | 56 | bool mode_union_find; 57 | bool mode_sssp; 58 | bool mode_minspanning_tree; 59 | bool mode_sssp_dij; 60 | bool mode_spsp; 61 | bool mode_complete; 62 | bool mode_ring; 63 | bool mode_optimal; 64 | bool mode_bipart; 65 | bool mode_wheel; 66 | bool mode_bell; 67 | bool mode_union=0; 68 | bool mode_eulerP=0; 69 | bool mode_term=0; 70 | #define RESET_PANEL_MODES mode_union_find=0,mode_sssp=0,mode_minspanning_tree=0,mode_sssp_dij=0,mode_spsp=0,mode_complete=0,mode_optimal=0,mode_complement=0,mode_ring=0,mode_bipart=0,mode_bell=0,mode_union=0,mode_eulerP=0,mode_term=0; 71 | 72 | 73 | 74 | int signal_LMB,signal_RMB; 75 | int signal_KEY; 76 | #define RESET_signal_MOUSE signal_LMB=0,signal_RMB=0; 77 | 78 | int mouse_on_Edge_button=0; 79 | int mouse_on_Vertex_button=0; 80 | int mouse_on_movenode_button=0; 81 | int mouse_on_addcost_button=0; 82 | int mouse_on_write_button=0; 83 | int mouse_on_clear_button=0; 84 | int mouse_on_read_button=0; 85 | int mouse_on_color_button=0; 86 | int mouse_on_level_button=0; 87 | #define RESET_mouse_on mouse_on_Edge_button=0; mouse_on_Vertex_button=0,mouse_on_movenode_button=0,mouse_on_addcost_button=0,mouse_on_write_button=0,mouse_on_clear_button=0,mouse_on_read_button=0,mouse_on_color_button=0,mouse_on_level_button=0;; 88 | #define COMMONS1 switch_sidebar2=OFF; switch_sidebar=OFF; RESET_BAR_MODES; 89 | int select_1=0; 90 | int select_2=0; 91 | #define RESET_SELECTION select_1=0,select_2=0,freeze=0; 92 | int select_for_move=0; 93 | int select_for_color=0; 94 | int select_for_level=0; 95 | int select_to_align=0; 96 | int select_for_wheel=0; 97 | int select1_spsp=0; 98 | int select2_spsp=0; 99 | int Total_nodes=0; 100 | int mouse_sensor_sidebar2=0; 101 | 102 | //dfs_info 103 | int Largest_union=0; 104 | int looper=0; 105 | 106 | ///sssp info 107 | int source_sssp; 108 | int sssp_dis[30]={0}; 109 | 110 | int sssp_static=0; 111 | int freeze=0; 112 | 113 | int source_sssp_dij; 114 | int sssp_dis_dij[30]={0}; 115 | int sssp_static_dij=0; 116 | 117 | char CHAR_PRESSED=0; 118 | string input_cost; 119 | string new_file_name; 120 | string new_level; 121 | int show_cost=0; 122 | int upd_bell[30]={0}; 123 | int enter_name_now=0; 124 | #define TEST REP(i,3)printf("%d ",i); puts(""); 125 | int spanning_cost=0; 126 | int Negative_cycle=1; 127 | int mode_delete=0; 128 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/temp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shafaet/Programming-Contest-Algorithms/42ffba423186976481c9c2f2852e58aa75294750/Graph Algorithm Simulator/temp -------------------------------------------------------------------------------- /Graph Algorithm Simulator/terminology.h: -------------------------------------------------------------------------------- 1 | void show_terms() 2 | { 3 | COLOR_DBLUE; 4 | class_SHAPE.FILLED_RECT(0,0,WIDTH,HEIGHT); 5 | string STR[30]; 6 | int head[50]={0}; 7 | STR[1].assign("BFS: BREADTH FIRST SEARCH"); head[1]=1; 8 | STR[2].assign("Bfs is an graph searching algorithm That begins at the root node and explores all the neighboring nodes."); 9 | STR[3].assign("Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal.(wiki)"); 10 | STR[4].assign("DFS: DEPTH FIRST SEARCH"); head[4]=1; 11 | STR[5].assign("Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. "); 12 | STR[6].assign("One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.(wiki)"); 13 | STR[7].assign("MST: Minimum Spanning Tree"); head[7]=1; 14 | STR[8].assign("Given a connected, undirected graph, a spanning tree of that graph is a subgraph which is a tree and connects all the vertices together."); 15 | STR[9].assign("A MST is then a spanning tree with weight less than or equal to the weight of every other spanning tree.(wiki)"); 16 | STR[10].assign("SSSP: Single Source Shortest Path"); head[10]=1; 17 | STR[11].assign("APSP: All pair Shortest Path"); head[11]=1; 18 | STR[12].assign("Dijkstra's Algorithm:"); head[12]=1; 19 | STR[13].assign("It is a graph search algorithm that solves the single-source shortest path problem"); 20 | STR[14].assign("for a graph with nonnegative edge path costs, producing a shortest path tree."); 21 | STR[15].assign("Euler Path:"); head[15]=1; 22 | STR[16].assign("An Eulerian trail is a trail in a graph which visits every edge exactly once."); 23 | STR[17].assign("Bellman Ford's algorithm(negative cycle finding):"); head[17]=1; 24 | STR[18].assign("The Bellman–Ford algorithm computes single-source shortest paths in a weighted digraph"); 25 | int down=0; 26 | for(int i=1; ;i++) 27 | { 28 | if(!STR[i].size()) break; 29 | 30 | if(head[i]){ COLOR_BROWN;down+=10; class_SHAPE.TEXT(60,HEIGHT-55-down,STR[i],font_large);} 31 | else{COLOR_WHITE;class_SHAPE.TEXT(60,HEIGHT-55-down,STR[i],font_medium);} 32 | down+=20; 33 | 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Graph Algorithm Simulator/untitled.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #define REP(i,n) for(__typeof(n) i=0; i<(n); i++) 18 | #define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++) 19 | #define INF (1<<30) 20 | #define eps 1e-9 21 | #define PB(x) push_back(x) 22 | 23 | #define VI vector 24 | #define VS vector 25 | #define LL long long 26 | 27 | #define MAX(a,b) (a>b?a:b) 28 | #define MIN(a,b) (a 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #define stream istringstream 25 | #define rep(i,n) for(__typeof(n) i=0; i<(n); i++) 26 | #define repl(i,n) for(__typeof(n) i=1; i<=(n); i++) 27 | #define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++) 28 | #define INF (1<<28) 29 | #define PI 3.14159265358979323846264338327950 30 | #define pb(x) push_back(x) 31 | #define ppb pop_back 32 | #define all(x) x.begin(),x.end() 33 | #define cclear(x,y) memset(x,y,sizeof(x)); 34 | #define mem(x,y) memset(x,y,sizeof(x)); 35 | #define eps 1e-9 36 | #define pii pair 37 | #define pmp make_pair 38 | 39 | 40 | #define SDi(x) scanf("%d",&x) 41 | #define SDii(x,y) scanf("%d%d",&x,&y) 42 | 43 | #define SDl(x) scanf("%lld",&x) 44 | #define SDs(x) scanf("%s",x) 45 | 46 | using namespace std; 47 | 48 | template inline T gcd(T a,T b) {if(a<0)return 49 | gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);} 50 | template inline T lcm(T a,T b) {if(a<0)return 51 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));} 52 | template inline T sqr(T x){return x*x;} 53 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 54 | template bool inside(T a,T b,T c){ return (b>=a && b<=c);} 55 | typedef long long i64; 56 | typedef unsigned long long ui64; 57 | 58 | #define READ(f) freopen(f, "r", stdin) 59 | #define WRITE(f) freopen(f, "w", stdout) 60 | const i64 INF64 = (i64)1E16; 61 | double dist3d(double x1,double y1,double z1,double x2,double y2,double z2){ return sqrt(sqr(x1-x2)+sqr(y1-y2)+sqr(z1-z2));} 62 | struct point 63 | { 64 | double x,y,z; 65 | point(double a,double b,double c) 66 | { 67 | x=a; 68 | y=b; 69 | z=c; 70 | } 71 | }; 72 | double get(double x1,double y1,double z1,double x2,double y2,double z2,double x3,double y3,double z3,double t) 73 | { 74 | point p1=point (x1+t*(x2-x1),y1+t*(y2-y1),z1+t*(z2-z1)); 75 | 76 | return dist3d(p1.x,p1.y,p1.z,x3,y3,z3); 77 | 78 | } 79 | int main() 80 | { 81 | //READ("in"); 82 | int t,kas=0; 83 | cin>>t; 84 | while(t--) 85 | { 86 | double x1,x2,x3,y1,y2,y3,z1,z2,z3; 87 | scanf("%lf%lf%lf",&x1,&y1,&z1); 88 | scanf("%lf%lf%lf",&x2,&y2,&z2); 89 | scanf("%lf%lf%lf",&x3,&y3,&z3); 90 | 91 | 92 | 93 | double lo=0,hi=1,ret1,ret2; 94 | int d = 64; 95 | while(d--) { 96 | double lt = (2 * lo + hi) / 3; 97 | double rt = (lo + 2 * hi) / 3; 98 | ret1=get(x1,y1,z1,x2,y2,z2,x3,y3,z3,lt); 99 | ret2=get(x1,y1,z1,x2,y2,z2,x3,y3,z3,rt); 100 | if(ret1>ret2) lo=lt; 101 | else hi=rt; 102 | } 103 | 104 | printf("Case %d: %.10lf\n",++kas,ret1); 105 | } 106 | 107 | 108 | return 0; 109 | } 110 | -------------------------------------------------------------------------------- /LightOJ/1095 - Arrange The Numbers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * LOJ 1095 Arrange the numbers 3 | * DP 4 | * Permutation with k non-fixed points 5 | * 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #define stream istringstream 30 | #define rep(i,n) for(int i=0; i<(int)n; i++) 31 | #define repv(i,n) for(int i=n-1; i>=0; i--) 32 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 33 | #define replv(i,n) for(int i=n; i>=1; i--) 34 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 35 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 36 | #define sz(x) (int)x.size() 37 | #define inf (1061109567) 38 | #define pb(x) push_back(x) 39 | #define ppb pop_back 40 | #define all(x) x.begin(),x.end() 41 | #define mem(x,y) memset(x,y,sizeof(x)); 42 | #define eps 1e-9 43 | #define rev reverse 44 | #define pii pair 45 | #define pll pair 46 | #define pmp make_pair 47 | #define sdi(x) scanf("%d",&x) 48 | #define sdii(x,y) scanf("%d%d",&x,&y) 49 | #define sds(x) scanf("%s",x) 50 | #define pfi(x) printf("%d\n",x); 51 | #define uu first 52 | #define vv second 53 | using namespace std; 54 | template inline T sqr(T x){return x*x;} 55 | template inline T lcm(T a,T b) {if(a<0)return 56 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 57 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 58 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 59 | typedef long long i64; 60 | typedef unsigned long long ui64; 61 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 62 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 63 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 64 | b.end(), *q ) ) q++;}return oot; 65 | } 66 | //bool operator < ( const node& p ) const { return w < p.w; } 67 | #define on(n,pos) (n | (1<<(pos))) 68 | #define off(n,pos) n & ~(1<=k); 102 | if(n<0 or k<0) return 0; 103 | if(k==0) return fact(n); 104 | if(dp[n][k]!=-1) return dp[n][k]; 105 | i64 norm=n-k; 106 | i64 sp=k-1; 107 | i64 ret1=0,ret2=0,ans=0; 108 | ret1=norm*f(n-1,k-1); 109 | ret2=sp* ((f(n-1,k-1) +f(n-2,k-2) )%mod); 110 | ans=ret1+ret2; 111 | ans%=mod; 112 | return dp[n][k]=ans; 113 | } 114 | 115 | 116 | int main() { 117 | 118 | //READ("in"); 119 | mem(dp,-1); 120 | int t,ks=0; 121 | sdi(t); 122 | while(t--) 123 | { 124 | int n,m,k; 125 | sdii(n,m); 126 | sdi(k); 127 | i64 ans=nCr(m,k)*f(n-k,m-k); 128 | ans%=mod; 129 | printf("Case %d: %lld\n",++ks,ans); 130 | } 131 | 132 | } 133 | -------------------------------------------------------------------------------- /LightOJ/1110 - An Easy LCS .cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Finding Lexicographically Minimum LCS 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #define stream istringstream 26 | #define rep(i,n) for(int i=0; i<(int)n; i++) 27 | #define repv(i,n) for(int i=n-1; i>=0; i--) 28 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 29 | #define replv(i,n) for(int i=n; i>=1; i--) 30 | 31 | 32 | #define INF (1<<28) 33 | #define PI 3.14159265358979323846264338327950 34 | #define pb(x) push_back(x) 35 | #define ppb pop_back 36 | #define all(x) x.begin(),x.end() 37 | #define mem(x,y) memset(x,y,sizeof(x)); 38 | #define eps 1e-9 39 | #define pii pair 40 | #define pmp make_pair 41 | 42 | 43 | #define sdi(x) scanf("%d",&x) 44 | #define sdl(x) scanf("%lld",&x) 45 | #define sdii(x,y) scanf("%d%d",&x,&y) 46 | #define SDs(x) scanf("%s",x) 47 | #define uu first 48 | #define vv second 49 | 50 | using namespace std; 51 | 52 | template inline T gcd(T a,T b) {if(a<0)return 53 | gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);} 54 | template inline T lcm(T a,T b) {if(a<0)return 55 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));} 56 | template inline T sqr(T x){return x*x;} 57 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 58 | 59 | typedef long long i64; 60 | typedef unsigned long long ui64; 61 | 62 | #define READ(f) freopen(f, "r", stdin) 63 | #define WRITE(f) freopen(f, "w", stdout) 64 | #define mx 202 65 | string a,b; 66 | int dp[mx][mx]; 67 | int call(int i,int j) 68 | { 69 | if(i>=(int)a.size() or j>=(int)b.size()) return 0; 70 | if(dp[i][j]!=-1) return dp[i][j]; 71 | if(a[i]==b[j]) 72 | { 73 | return dp[i][j]=call(i+1,j+1)+1; 74 | } 75 | 76 | return dp[i][j]=max(call(i+1,j),call(i,j+1)); 77 | 78 | } 79 | vectorsava[100],savb[100]; 80 | 81 | string out; 82 | void print(int i,int j,int len) 83 | { 84 | if(len==0) return; 85 | for(char ch='a';ch<='z';ch++) 86 | { 87 | int pi=lower_bound(all(sava[ch-'a']),i)-sava[ch-'a'].begin(); 88 | if(pi!=(int)sava[ch-'a'].size()) 89 | { 90 | int pj=lower_bound(all(savb[ch-'a']),j)-savb[ch-'a'].begin(); 91 | if(pj!=(int)savb[ch-'a'].size()) 92 | { 93 | pi=sava[ch-'a'][pi]; 94 | pj=savb[ch-'a'][pj]; 95 | if(call(pi+1,pj+1)==len-1) 96 | { 97 | out+=ch; 98 | print(pi+1,pj+1,len-1); 99 | break; 100 | } 101 | } 102 | } 103 | } 104 | 105 | } 106 | int main() 107 | { 108 | //READ("in"); 109 | int t,kas=0; 110 | cin>>t; 111 | while(t--) 112 | { 113 | mem(dp,-1); 114 | rep(i,100){ 115 | sava[i].clear(); 116 | savb[i].clear(); 117 | } 118 | cin>>a>>b; 119 | 120 | rep(i,a.size())sava[a[i]-'a'].pb(i); 121 | rep(i,b.size())savb[b[i]-'a'].pb(i); 122 | out=""; 123 | int ret=call(0,0); 124 | if(ret==0) out=":("; 125 | else 126 | print(0,0,ret); 127 | printf("Case %d: ",++kas); 128 | cout< 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #define stream istringstream 27 | #define rep(i,n) for(int i=0; i<(int)n; i++) 28 | #define repv(i,n) for(int i=n-1; i>=0; i--) 29 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 30 | #define replv(i,n) for(int i=n; i>=1; i--) 31 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 32 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 33 | #define sz(x) (int)x.size() 34 | #define inf (1061109567) 35 | #define INT_MAX 2147483647 36 | #define pb(x) push_back(x) 37 | #define ppb pop_back 38 | #define all(x) x.begin(),x.end() 39 | #define mem(x,y) memset(x,y,sizeof(x)); 40 | #define eps 1e-9 41 | #define rev reverse 42 | #define pii pair 43 | #define pll pair 44 | #define pmp make_pair 45 | #define sdi(x) scanf("%d",&x) 46 | #define sdii(x,y) scanf("%d%d",&x,&y) 47 | #define sds(x) scanf("%s",x) 48 | #define pfi(x) printf("%d\n",x); 49 | #define uu first 50 | #define vv second 51 | using namespace std; 52 | template inline T sqr(T x){return x*x;} 53 | template inline T lcm(T a,T b) {if(a<0)return 54 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 55 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 56 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 57 | typedef long long i64; 58 | typedef unsigned long long ui64; 59 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 60 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 61 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 62 | b.end(), *q ) ) q++;}return oot; 63 | } 64 | int fx[]={0,0,1,-1}; 65 | int fy[]={-1,1,0,0}; 66 | bool eq(double a,double b){return fabs(a-b)=sz(a) or j>=sz(b)) return 1; 96 | if(sav[i][j]!=-1) return sav[i][j]; 97 | int lc=call(i,j); 98 | 99 | if(lc==0) return sav[i][j]=1; 100 | 101 | int ans=0; 102 | rep(k,26) 103 | { 104 | int x=nexta[i][k]; 105 | int y=nextb[j][k]; 106 | 107 | if(call(x,y)==lc) 108 | { 109 | 110 | ans+=count(x+1,y+1); 111 | ans%=mod; 112 | } 113 | } 114 | return sav[i][j]=ans; 115 | } 116 | 117 | int main() 118 | { 119 | //READ("in"); 120 | int t,ks=0; 121 | sdi(t); 122 | while(t--) 123 | { 124 | mem(dp,-1); 125 | mem(sav,-1); 126 | cin>>a>>b; 127 | 128 | mem(nexta,-1); 129 | mem(nextb,-1); 130 | 131 | int last[33]; 132 | rep(i,26)last[i]=sz(a); 133 | repv(i,sz(a)) 134 | { 135 | int x=a[i]-'a'; 136 | last[x]=i; 137 | rep(j,26) 138 | nexta[i][j]=last[j]; 139 | } 140 | rep(i,26)last[i]=sz(b); 141 | 142 | repv(i,sz(b)) 143 | { 144 | int x=b[i]-'a'; 145 | last[x]=i; 146 | rep(j,26) 147 | nextb[i][j]=last[j]; 148 | } 149 | pks; 150 | pfi(count(0,0)); 151 | 152 | } 153 | 154 | } 155 | 156 | -------------------------------------------------------------------------------- /Others/Andrew-Stankevich-10C-Order Preserving Codes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 2004-2005 Winter Petrozavodsk Camp, Andrew Stankevich Contest 10 3 | * DP,knuth Optimization,path printing 4 | * O(n^2) 5 | */ 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #define stream istringstream 29 | #define rep(i,n) for(int i=0; i<(int)n; i++) 30 | #define repv(i,n) for(int i=n-1; i>=0; i--) 31 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 32 | #define replv(i,n) for(int i=n; i>=1; i--) 33 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 34 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 35 | #define sz(x) (int)x.size() 36 | #define inf (1061109567) 37 | #define pb(x) push_back(x) 38 | #define ppb pop_back 39 | #define all(x) x.begin(),x.end() 40 | #define mem(x,y) memset(x,y,sizeof(x)); 41 | #define eps 1e-9 42 | #define rev reverse 43 | #define pii pair 44 | #define pll pair 45 | #define pmp make_pair 46 | #define sdi(x) scanf("%d",&x) 47 | #define sdii(x,y) scanf("%d%d",&x,&y) 48 | #define sds(x) scanf("%s",x) 49 | #define pfi(x) printf("%d\n",x); 50 | #define uu first 51 | #define vv second 52 | using namespace std; 53 | template inline T sqr(T x){return x*x;} 54 | template inline T lcm(T a,T b) {if(a<0)return 55 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 56 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 57 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 58 | typedef long long i64; 59 | typedef unsigned long long ui64; 60 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 61 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 62 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 63 | b.end(), *q ) ) q++;}return oot; 64 | } 65 | //bool operator < ( const node& p ) const { return w < p.w; } 66 | #define on(n,pos) (n | (1<<(pos))) 67 | #define off(n,pos) n & ~(1<b) return 0; 80 | return s[b]-s[a-1]; 81 | } 82 | 83 | i64 dp[mx][mx]; 84 | int mid[mx][mx]; 85 | i64 iter(int n) 86 | { 87 | for(int s=1;s<=n;s++) 88 | { 89 | for(int a=1;a+s-1<=n;a++) 90 | { 91 | int b=a+s-1; 92 | if(a==b) 93 | { 94 | dp[a][b]=0; 95 | mid[a][b]=a; 96 | continue; 97 | } 98 | dp[a][b]=1LL<<52LL; 99 | for(int m=mid[a][b-1];m<=mid[a+1][b];m++) 100 | { 101 | i64 ret=dp[a][m]+dp[m+1][b]+cost(a,b); 102 | if(ret=b){ 119 | out[c]=0; 120 | printf("%s\n",out); 121 | return; 122 | } 123 | 124 | int m=mid[a][b]; 125 | out[c++]='0'; 126 | 127 | print(a,m); 128 | c--; 129 | 130 | out[c++]='1'; 131 | print(m+1,b); 132 | c--; 133 | } 134 | 135 | int main() { 136 | 137 | READ("codes.in"); 138 | WRITE("codes.out"); 139 | int n; 140 | while(cin>>n) 141 | { 142 | repl(i,n)cin>>f[i],s[i]=s[i-1]+f[i]; 143 | 144 | iter(n); 145 | print(1,n); 146 | 147 | } 148 | 149 | 150 | } 151 | -------------------------------------------------------------------------------- /Others/NorwayNCPC2012Juice.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define stream istringstream 24 | #define rep(i,n) for(int i=0; i<(int)n; i++) 25 | #define repv(i,n) for(int i=n-1; i>=0; i--) 26 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 27 | #define replv(i,n) for(int i=n; i>=1; i--) 28 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 29 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 30 | #define sz(x) (int)x.size() 31 | #define inf (1061109567) 32 | #define pb(x) push_back(x) 33 | #define ppb pop_back 34 | #define all(x) x.begin(),x.end() 35 | #define mem(x,y) memset(x,y,sizeof(x)); 36 | #define eps 1e-9 37 | #define pii pair 38 | #define pll pair 39 | #define pmp make_pair 40 | #define sdi(x) scanf("%d",&x) 41 | #define sdii(x,y) scanf("%d%d",&x,&y) 42 | #define sds(x) scanf("%s",x) 43 | #define pfi(x) printf("%d\n",x); 44 | #define uu first 45 | #define vv second 46 | using namespace std; 47 | template inline T sqr(T x){return x*x;} 48 | template inline T lcm(T a,T b) {if(a<0)return 49 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 50 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 51 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 52 | typedef long long i64; 53 | typedef unsigned long long ui64; 54 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 55 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 56 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 57 | b.end(), *q ) ) q++;}return oot; 58 | } 59 | //bool operator < ( const node& p ) const { return w < p.w; } 60 | #define on(n,pos) (n | (1<g[1002]; 71 | int n; 72 | int need[1002]; 73 | int dp[1002][102][102]; 74 | bool vis[1002][102][102]; 75 | int call(int u,int e,int power) 76 | { 77 | if(e>=sz(g[u])) return power>=need[u]; 78 | if(vis[u][e][power]) return dp[u][e][power]; 79 | vis[u][e][power]=1; 80 | int ans=0; 81 | for(int d=0;d<=power and d<=g[u][e].second;d++) 82 | { 83 | int v=g[u][e].first; 84 | int ret=call(v,0,d)+call(u,e+1,power-d); 85 | ans=max(ans,ret); 86 | } 87 | return dp[u][e][power]=ans; 88 | } 89 | int main(){ 90 | 91 | //READ("data/juice/juice.02.in"); 92 | 93 | cin>>n; 94 | repl(i,n) 95 | { 96 | int v,cap; 97 | sdi(v); 98 | sdi(need[i]); 99 | sdi(cap); 100 | g[v].pb(pii(i,cap)); 101 | 102 | } 103 | int ans=0; 104 | rep(i,sz(g[0])) 105 | { 106 | int u=g[0][i].first; 107 | int cap=g[0][i].second; 108 | ans+=call(u,0,cap); 109 | 110 | } 111 | pfi(ans); 112 | return 0; 113 | } 114 | 115 | -------------------------------------------------------------------------------- /Others/Tree Generator.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Shafaet 3 | University of Dhaka 4 | Shafaetsplanet.com 5 | 6 | Generates a weighted tree with n node 7 | 8 | */ 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #define stream istringstream 32 | #define rep(i,n) for(int i=0; i<(int)n; i++) 33 | #define repv(i,n) for(int i=n-1; i>=0; i--) 34 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 35 | #define replv(i,n) for(int i=n; i>=1; i--) 36 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 37 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 38 | #define SZ(x) (int)x.size() 39 | #define INF (1061109567) 40 | #define pb(x) push_back(x) 41 | #define ppb pop_back 42 | #define all(x) x.begin(),x.end() 43 | #define mem(x,y) memset(x,y,sizeof(x)); 44 | #define eps 1e-9 45 | #define pii pair 46 | #define pll pair 47 | #define pmp make_pair 48 | #define sdi(x) scanf("%d",&x) 49 | #define sdii(x,y) scanf("%d%d",&x,&y) 50 | #define sds(x) scanf("%s",x) 51 | #define pfi(x) printf("%d\n",x); 52 | #define uu first 53 | #define vv second 54 | using namespace std; 55 | 56 | #define READ(f) freopen(f, "r", stdin) 57 | #define WRITE(f) freopen(f, "w", stdout) 58 | #define pks printf("Case #%d: ",++ks); 59 | 60 | 61 | #define mx 1002 62 | 63 | 64 | struct edg 65 | { 66 | int u,v; 67 | edg(int u,int v):u(u),v(v){} 68 | }; 69 | int pr[mx]; 70 | int find(int r) 71 | { 72 | if(pr[r]==r) return r; 73 | return pr[r]=find(pr[r]); 74 | } 75 | void add(edg x) 76 | { 77 | if(find(x.u)!=find(x.v)) 78 | pr[find(x.u)]=find(x.v); 79 | } 80 | int rnd(int n){return rand()%n+1;} 81 | 82 | int weight() 83 | { 84 | int x=rand()%100; 85 | int p=rand()%10; 86 | if(p>6) x*=-1; 87 | return x; 88 | } 89 | edg getedge(int n) 90 | { 91 | int u=rnd(n); 92 | int v=rnd(n); 93 | while(v==u)v=rnd(n); 94 | return edg(u,v); 95 | } 96 | 97 | bool tkn[mx][mx]; 98 | void gentree(int n) 99 | { 100 | cout< 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #define stream istringstream 25 | #define rep(i,n) for(int i=0; i<(int)n; i++) 26 | #define repv(i,n) for(int i=n-1; i>=0; i--) 27 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 28 | #define replv(i,n) for(int i=n; i>=1; i--) 29 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 30 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 31 | #define sz(x) (int)x.size() 32 | #define inf (1061109567) 33 | #define pb(x) push_back(x) 34 | #define ppb pop_back 35 | #define all(x) x.begin(),x.end() 36 | #define mem(x,y) memset(x,y,sizeof(x)); 37 | #define eps 1e-9 38 | #define rev reverse 39 | #define pii pair 40 | #define pll pair 41 | #define pmp make_pair 42 | #define sdi(x) scanf("%d",&x) 43 | #define sdii(x,y) scanf("%d%d",&x,&y) 44 | #define sds(x) scanf("%s",x) 45 | #define pfi(x) printf("%d\n",x); 46 | #define uu first 47 | #define vv second 48 | using namespace std; 49 | template inline T sqr(T x){return x*x;} 50 | template inline T lcm(T a,T b) {if(a<0)return 51 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 52 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 53 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 54 | typedef long long i64; 55 | typedef unsigned long long ui64; 56 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 57 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 58 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 59 | b.end(), *q ) ) q++;}return oot; 60 | } 61 | //bool operator < ( const node& p ) const { return w < p.w; } 62 | #define on(n,pos) (n | (1<100) return 0; 82 | if(i==n or j==n) return 0; 83 | 84 | int off=max(i-100,0); 85 | if(vis[i][j-off]==cc) return dp[i][j-off]; 86 | vis[i][j-off]=cc; 87 | 88 | if(a[i]==b[j]) return dp[i][j-off]=1+call(i+1,j+1); 89 | else return dp[i][j-off]=max(call(i+1,j),call(i,j+1)); 90 | } 91 | int main(){ 92 | //READ("palindrome.in"); 93 | //WRITE("out"); 94 | int t,ks=0; 95 | cin>>t; 96 | while(t--) 97 | { 98 | ++cc; 99 | 100 | sdi(n); 101 | rep(i,n)sdi(a[i]),b[i]=a[i]; 102 | reverse(b,b+n); 103 | int ret=call(0,0); 104 | printf("Sequence #%d: %d\n",++ks,n-ret); 105 | puts(""); 106 | 107 | } 108 | 109 | return 0; 110 | } 111 | 112 | -------------------------------------------------------------------------------- /Others/USACO-MAR08-ACQUIRE.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * USACO MAR08 Land Acquisition(ACQUIRE) 3 | * DP with convex hull optimization 4 | * http://wcipeg.com/wiki/Convex_hull_trick 5 | * http://cerberus.delos.com:790/TESTDATA/MAR08.acquire.htm 6 | * SPOJ ACQUIRE 7 | 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #define stream istringstream 33 | #define rep(i,n) for(int i=0; i<(int)n; i++) 34 | #define repv(i,n) for(int i=n-1; i>=0; i--) 35 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 36 | #define replv(i,n) for(int i=n; i>=1; i--) 37 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 38 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 39 | #define sz(x) (int)x.size() 40 | #define inf (1061109567) 41 | #define pb(x) push_back(x) 42 | #define ppb pop_back 43 | #define all(x) x.begin(),x.end() 44 | #define mem(x,y) memset(x,y,sizeof(x)); 45 | #define eps 1e-9 46 | #define rev reverse 47 | #define pii pair 48 | #define pll pair 49 | #define pmp make_pair 50 | #define sdi(x) scanf("%d",&x) 51 | #define sdii(x,y) scanf("%d%d",&x,&y) 52 | #define sds(x) scanf("%s",x) 53 | #define pfi(x) printf("%d\n",x); 54 | #define uu first 55 | #define vv second 56 | using namespace std; 57 | typedef long long i64; 58 | typedef unsigned long long ui64; 59 | #define READ(f) freopen(f, "r", stdin) 60 | #define WRITE(f) freopen(f, "w", stdout) 61 | #define pks printf("Case %d: ",++ks); 62 | 63 | #define MAX 100002 64 | struct rect { long long w, h; }plots[MAX]; 65 | struct line 66 | { 67 | long long m, b; 68 | line(long long x=0, long long y=0) { m=x; b=y; } 69 | }lines[MAX]; 70 | 71 | 72 | bool cmp(const rect &a,const rect &b) 73 | { 74 | //width increasing 75 | //height decreasing 76 | if(a.w==b.w) 77 | return a.h>b.h; 78 | return a.w=p2; 86 | 87 | } 88 | void addline(i64 m,i64 b,int startptr,int &endptr) // 0 based 89 | { 90 | lines[endptr]=line(m,b); 91 | endptr++; 92 | while (endptr-startptr >= 3 && bad(lines[endptr-3], lines[endptr-2], lines[endptr-1])) //check if we can eliminate second last line 93 | { 94 | lines[endptr-2] = lines[endptr-1]; 95 | endptr--; 96 | } 97 | } 98 | i64 dp[MAX]; 99 | int main() { 100 | //READ("acquire/acquire.10.in"); 101 | ios_base::sync_with_stdio(false); 102 | int n; 103 | cin>>n; 104 | rep(i,n) 105 | { 106 | cin>>plots[i].w>>plots[i].h; 107 | } 108 | sort(plots,plots+n,cmp); 109 | //dp=b; 110 | //plots.h=m 111 | //plots.w=x 112 | //minimize y=mx+b 113 | 114 | 115 | int c=0; 116 | rep(i,n) 117 | { 118 | plots[c]=plots[i]; 119 | while (c && plots[c].w >= plots[c-1].w and plots[c].h >= plots[c-1].h) //If one box can be put into other, remove it 120 | { 121 | plots[c-1] = plots[c]; 122 | c--; 123 | } 124 | c++; 125 | } 126 | n=c; 127 | 128 | //Segments which are left to the current points are not needed to search 129 | //So we can use two pointers instead in bsearch 130 | int start=0,end=1; 131 | dp[0]=0; 132 | lines[0]=line(plots[0].h,dp[0]); 133 | rep(i,n) 134 | { 135 | dp[i+1]=1LL<<60LL; 136 | for(int j=start;j 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #define stream istringstream 29 | #define rep(i,n) for(int i=0; i<(int)n; i++) 30 | #define repv(i,n) for(int i=n-1; i>=0; i--) 31 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 32 | #define replv(i,n) for(int i=n; i>=1; i--) 33 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 34 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 35 | #define sz(x) (int)x.size() 36 | #define inf (1061109567) 37 | #define pb(x) push_back(x) 38 | #define ppb pop_back 39 | #define all(x) x.begin(),x.end() 40 | #define mem(x,y) memset(x,y,sizeof(x)); 41 | #define eps 1e-9 42 | #define rev reverse 43 | #define pii pair 44 | #define pll pair 45 | #define pmp make_pair 46 | #define sdi(x) scanf("%d",&x) 47 | #define sdii(x,y) scanf("%d%d",&x,&y) 48 | #define sds(x) scanf("%s",x) 49 | #define pfi(x) printf("%d\n",x); 50 | #define uu first 51 | #define vv second 52 | using namespace std; 53 | template inline T sqr(T x){return x*x;} 54 | template inline T lcm(T a,T b) {if(a<0)return 55 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 56 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 57 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 58 | typedef long long i64; 59 | typedef unsigned long long ui64; 60 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 61 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 62 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 63 | b.end(), *q ) ) q++;}return oot; 64 | } 65 | //bool operator < ( const node& p ) const { return w < p.w; } 66 | #define on(n,pos) (n | (1<<(pos))) 67 | #define off(n,pos) n & ~(1<=1) u=ptr[a-1]+1; 82 | else u=1; 83 | 84 | if(b+1<=k) v=ptr[b+1]; 85 | else v=n; 86 | return v-u+1; 87 | } 88 | i64 dp[1020][1020]; 89 | int mid[1200][1020]; 90 | i64 iter() 91 | { 92 | 93 | for(int s=1;s<=k;s++) 94 | { 95 | for(int L=1;L+s-1<=k;L++) 96 | { 97 | int R=L+s-1; 98 | if(L==R) 99 | { 100 | dp[L][R]=len(L,R); 101 | mid[L][R]=L; 102 | continue; 103 | } 104 | dp[L][R]=1LL<<60LL; 105 | for(int m=mid[L][R-1];m<=mid[L+1][R];m++) //Run over mid points 106 | { 107 | i64 ret=dp[L][m-1]+dp[m+1][R]+len(L,R); 108 | if(ret>n>>k) 126 | { 127 | assert(k=1 and ptr[i] 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #define stream istringstream 26 | #define rep(i,n) for(int i=0; i<(int)n; i++) 27 | #define repv(i,n) for(int i=n-1; i>=0; i--) 28 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 29 | #define replv(i,n) for(int i=n; i>=1; i--) 30 | 31 | 32 | #define INF (1<<28) 33 | #define PI 3.14159265358979323846264338327950 34 | #define pb(x) push_back(x) 35 | #define ppb pop_back 36 | #define all(x) x.begin(),x.end() 37 | #define mem(x,y) memset(x,y,sizeof(x)); 38 | #define eps 1e-9 39 | #define pii pair 40 | #define pmp make_pair 41 | 42 | 43 | #define sdi(x) scanf("%d",&x) 44 | #define sdii(x,y) scanf("%d%d",&x,&y) 45 | #define SDs(x) scanf("%s",x) 46 | #define uu first 47 | #define vv second 48 | 49 | using namespace std; 50 | 51 | template inline T gcd(T a,T b) {if(a<0)return 52 | gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);} 53 | template inline T lcm(T a,T b) {if(a<0)return 54 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));} 55 | template inline T sqr(T x){return x*x;} 56 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 57 | 58 | typedef long long i64; 59 | typedef unsigned long long ui64; 60 | 61 | #define READ(f) freopen(f, "r", stdin) 62 | #define WRITE(f) freopen(f, "w", stdout) 63 | 64 | i64 toInt64(string s){i64 r=0;istringstream sin(s);sin>>r;return r;} 65 | double LOG(i64 N,i64 B){ return (log10l(N))/(log10l(B)); } 66 | string itoa(i64 a){if(a==0) return "0";string ret;for(i64 i=a; i>0; i=i/10) ret.push_back((i%10)+48);reverse(all(ret));return ret;} 67 | 68 | vector< string > token( string a, string b ) { 69 | const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 70 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), 71 | *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 72 | b.end(), *q ) ) q++;}return oot; 73 | } 74 | 75 | //bool operator < ( const node& p ) const { return w < p.w; } 76 | 77 | int fx[]={0,0,-1,+1,-1,-1,+1,+1}; 78 | int fy[]={-1,+1,0,0,-1,+1,-1,+1}; 79 | 80 | int Set(int N,int pos){return N=N | (1<=0;i--) {bool x=check(N,i);cout<>1; 98 | init(Left,b,mid); 99 | init(Right,mid+1,e); 100 | 101 | tree[node]=tree[Left]+tree[Right]; 102 | } 103 | int query(int node,int b,int e,int k) 104 | { 105 | 106 | if(b==e) { 107 | tree[node]--; 108 | return b; 109 | } 110 | int Left=node<<1; 111 | int Right=Left+1; 112 | int mid=(b+e)>>1; 113 | 114 | int ret; 115 | if(tree[Right]>=k) ret= query(Right,mid+1,e,k); 116 | else ret=query(Left,b,mid,k-tree[Right]); 117 | tree[node]=tree[Left]+tree[Right]; 118 | return ret; 119 | 120 | } 121 | int a[mx]; 122 | int main(){ 123 | //READ("in"); 124 | 125 | int n; 126 | sdi(n); 127 | repl(i,n){ 128 | if(i==1) a[i]=0; 129 | else { 130 | sdi(a[i]); 131 | int baam=i-1; 132 | a[i]=baam-a[i]; 133 | } 134 | 135 | } 136 | init(1,1,n); 137 | 138 | int b[mx]; 139 | for(int i=n;i>=1;i--) b[i]=query(1,1,n,a[i]+1); 140 | 141 | repl(i,n) 142 | printf("%d\n",b[i]); 143 | 144 | 145 | 146 | 147 | return 0; 148 | 149 | } 150 | 151 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **Problem Solving in Online Judge** 2 | 3 | This repository contains some frequently used algorithms for competitive programming. 4 | 5 | Author: Shafaet Ashraf 6 | Shafaet.csedu@gmail.com 7 | 8 | 9 | ***BASIC SUBROUTINES*** 10 | ------------ 11 | 12 | **Graph** 13 | ------------ 14 | 15 | **DFS cycle finding** 16 | 17 | - SPOJ Paradox 18 | 19 | **Max Flow** 20 | 21 | - POJ 3204- Ikki's Story I - Road Reconstruction 22 | 23 | **Min Cost Max Flow** 24 | 25 | - UVA 12092 - Paint the Roads 26 | 27 | **Bipartite Matching(Konigs Theorem)** 28 | 29 | - UVA 11419 - Sam i am 30 | 31 | **Prufer Codes** 32 | 33 | - Timus 1069 Prufer Code 34 | 35 | **Strongly Connected components** 36 | 37 | - Topcoder - BikeRiding(SRM 545 div2 level 3) 38 | 39 | **Bridge** 40 | 41 | - UVA 1310 (One way traffic) 42 | 43 | 44 | **Dynamic Programming** 45 | ------------------------ 46 | 47 | **Knuth Optimization** 48 | 49 | - UVA 10304-Optimal Binary Search Tree 50 | - ZOJ 2860 Breaking Strings 51 | - Andrew-Stankevich-10C-Order Preserving Codes 52 | 53 | **Convex Hull Optimization** 54 | 55 | - APIO10-commandos 56 | - USACO-MAR08-ACQUIRE 57 | 58 | **LCS Variants** 59 | 60 | - Light OJ 1110 - An Easy LCS (Printing Lexicographically Smallest LCS) 61 | - Light OJ 1157 - LCS Revisited (Number of Unique LCS) 62 | 63 | 64 | 65 | **Data Structure** 66 | ----------------------- 67 | 68 | **Heavy Light Decomposition** 69 | 70 | - Timus 1553 Caves and Tunnels 71 | - UVA 12424 Answering Queries On A Tree 72 | 73 | 74 | **RMQ using Sparse Table** 75 | 76 | - POJ 2019 USACO-Cornfield 77 | 78 | **Balanced Binary Search Tree(TREAP)** 79 | 80 | - SPOJ QMAX3VN 81 | 82 | **2D Binary Indexed Tree** 83 | 84 | - LightOJ 1266 - Points in Rectangle 85 | 86 | **2D Segment Tree** 87 | 88 | - UVA 11297 Census 89 | 90 | **Lowest Common Ancestor Using Sparse Table*** 91 | 92 | - LightOJ 1162 - Min Max Roads 93 | 94 | 95 | **Matrix Exponentiation** 96 | --------------------- 97 | 98 | - SPOJ SNAKYNUM 99 | - LightOJ 1268 - Unlucky String 100 | 101 | 102 | **Geometry** 103 | --------------------- 104 | 105 | **Max Co-linear Points** 106 | 107 | - Timus 1052 Rabbit Hunt 108 | 109 | **Point Segment Distance (3D)** 110 | 111 | - LightOJ 1240 - Point Segment Distance (3D) 112 | 113 | 114 | **String Algorithms** 115 | --------------------- 116 | 117 | **KMP** 118 | 119 | - LightOJ 1268 - Unlucky String 120 | 121 | 122 | **Gaussian Elimination** 123 | --------------------- 124 | 125 | **Bitwise Gaussian Elimination over gf2** 126 | 127 | - UVALIVE 5070 Awkward Lights 128 | 129 | 130 | **Others** 131 | --------------------- 132 | 133 | **Permutation with k non-fixed points** 134 | - LightOJ 1095 - Arrange The Numbers 135 | 136 | **Prime factorization inside sieve of eratosthenes** 137 | - UVALIVE 4735- Not So Flat After All 138 | 139 | **Longest Increasing Subsequence in O(nlogk)** 140 | 141 | - UVA 11031 - Looking for a Subset 142 | 143 | **Ternary Search** 144 | 145 | - LightOJ 1240 - Point Segment Distance (3D) 146 | 147 | 148 | **2D Grid Compression** 149 | 150 | - UVA 11918 - Traveler of Gridland 151 | - UVA 12069 - Robots inside the Labyrinth 152 | -------------------------------------------------------------------------------- /README.md~: -------------------------------------------------------------------------------- 1 | **Problem Solving in Online Judge** 2 | 3 | This repository contains some frequently used algorithms for competitive programming. 4 | 5 | Author: Shafaet Ashraf 6 | Shafaet.csedu@gmail.com 7 | 8 | 9 | ***BASIC SUBROUTINES*** 10 | ------------ 11 | 12 | **Graph** 13 | ------------ 14 | 15 | **Max Flow** 16 | 17 | - POJ 3204- Ikki's Story I - Road Reconstruction 18 | 19 | **Min Cost Max Flow** 20 | 21 | - UVA 12092 - Paint the Roads 22 | 23 | **Bipartite Matching(Konigs Theorem)** 24 | 25 | - UVA 11419 - Sam i am 26 | 27 | **Prufer Codes** 28 | 29 | - Timus 1069 Prufer Code 30 | 31 | **Strongly Connected components** 32 | 33 | - Topcoder - BikeRiding(SRM 545 div2 level 3) 34 | 35 | **Bridge** 36 | 37 | - UVA 1310 (One way traffic) 38 | 39 | 40 | **Dynamic Programming** 41 | ------------------------ 42 | 43 | **Knuth Optimization** 44 | 45 | - UVA 10304-Optimal Binary Search Tree 46 | - ZOJ 2860 Breaking Strings 47 | - Andrew-Stankevich-10C-Order Preserving Codes 48 | 49 | **Convex Hull Optimization** 50 | 51 | - APIO10-commandos 52 | - USACO-MAR08-ACQUIRE 53 | 54 | **LCS Variants** 55 | 56 | - Light OJ 1110 - An Easy LCS (Printing Lexicographically Smallest LCS) 57 | - Light OJ 1157 - LCS Revisited (Number of Unique LCS) 58 | 59 | 60 | 61 | **Data Structure** 62 | ----------------------- 63 | 64 | **Heavy Light Decomposition** 65 | 66 | - Timus 1553 Caves and Tunnels 67 | - UVA 12424 Answering Queries On A Tree 68 | 69 | 70 | **RMQ using Sparse Table** 71 | 72 | - POJ 2019 USACO-Cornfield 73 | 74 | **Balanced Binary Search Tree(TREAP)** 75 | 76 | - SPOJ QMAX3VN 77 | 78 | **2D Binary Indexed Tree** 79 | 80 | - LightOJ 1266 - Points in Rectangle 81 | 82 | **2D Segment Tree** 83 | 84 | - UVA 11297 Census 85 | 86 | **Lowest Common Ancestor Using Sparse Table*** 87 | 88 | - LightOJ 1162 - Min Max Roads 89 | 90 | 91 | **Matrix Exponentiation** 92 | --------------------- 93 | 94 | - SPOJ SNAKYNUM 95 | - LightOJ 1268 - Unlucky String 96 | 97 | 98 | **Geometry** 99 | --------------------- 100 | 101 | **Max Co-linear Points** 102 | 103 | - Timus 1052 Rabbit Hunt 104 | 105 | **Point Segment Distance (3D)** 106 | 107 | - LightOJ 1240 - Point Segment Distance (3D) 108 | 109 | 110 | **String Algorithms** 111 | --------------------- 112 | 113 | **KMP** 114 | 115 | - LightOJ 1268 - Unlucky String 116 | 117 | 118 | **Gaussian Elimination** 119 | --------------------- 120 | 121 | **Bitwise Gaussian Elimination over gf2** 122 | 123 | - UVALIVE 5070 Awkward Lights 124 | 125 | 126 | **Others** 127 | --------------------- 128 | 129 | **Permutation with k non-fixed points** 130 | - LightOJ 1095 - Arrange The Numbers 131 | 132 | **Prime factorization inside sieve of eratosthenes** 133 | - UVALIVE 4735- Not So Flat After All 134 | 135 | **Longest Increasing Subsequence in O(nlogk)** 136 | 137 | - UVA 11031 - Looking for a Subset 138 | 139 | **Ternary Search** 140 | 141 | - LightOJ 1240 - Point Segment Distance (3D) 142 | 143 | 144 | **2D Grid Compression** 145 | 146 | - UVA 11918 - Traveler of Gridland 147 | - UVA 12069 - Robots inside the Labyrinth 148 | -------------------------------------------------------------------------------- /SPOJ/EXPRESS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define stream istringstream 24 | #define rep(i,n) for(int i=0; i<(int)n; i++) 25 | #define repv(i,n) for(int i=n-1; i>=0; i--) 26 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 27 | #define replv(i,n) for(int i=n; i>=1; i--) 28 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 29 | #define SZ(x) (int)x.size() 30 | #define INF (1061109567) 31 | #define pb(x) push_back(x) 32 | #define ppb pop_back 33 | #define all(x) x.begin(),x.end() 34 | #define mem(x,y) memset(x,y,sizeof(x)); 35 | #define eps 1e-9 36 | #define pii pair 37 | #define pll pair 38 | #define pmp make_pair 39 | #define sdi(x) scanf("%d",&x) 40 | #define sdii(x,y) scanf("%d%d",&x,&y) 41 | #define sds(x) scanf("%s",x) 42 | #define pfi(x) printf("%d\n",x); 43 | #define uu first 44 | #define vv second 45 | using namespace std; 46 | template inline T sqr(T x){return x*x;} 47 | 48 | template inline T lcm(T a,T b) {if(a<0)return 49 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 50 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 51 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 52 | typedef long long i64; 53 | typedef unsigned long long ui64; 54 | 55 | 56 | #define READ(f) freopen(f, "r", stdin) 57 | #define WRITE(f) freopen(f, "w", stdout) 58 | #define pks printf("Case #%lld: ",++ks); 59 | 60 | 61 | 62 | #define mx 100012 63 | 64 | int main() 65 | { 66 | //READ("in"); 67 | int t; 68 | sdi(t); 69 | while(t--) 70 | { 71 | pii tree[mx]; 72 | mem(tree,-1); 73 | stackst; 74 | char s[mx]; 75 | sds(s); 76 | for(int i=0;s[i];i++) 77 | { 78 | if(islower(s[i])) 79 | { 80 | st.push(i); 81 | } 82 | else 83 | { 84 | 85 | tree[i].second=st.top(); st.pop(); 86 | tree[i].first=st.top(); st.pop(); 87 | st.push(i); 88 | } 89 | } 90 | 91 | queueq; 92 | q.push(st.top()); st.pop(); 93 | string out; 94 | while(!q.empty()) 95 | { 96 | int u=q.front(); q.pop(); 97 | out+=s[u]; 98 | 99 | int v=tree[u].first; 100 | int w=tree[u].second; 101 | if(v!=-1) q.push(v); 102 | if(w!=-1) q.push(w); 103 | } 104 | reverse(all(out)); 105 | cout< 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #define stream istringstream 26 | #define rep(i,n) for(int i=0; i<(int)n; i++) 27 | #define repv(i,n) for(int i=n-1; i>=0; i--) 28 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 29 | #define replv(i,n) for(int i=n; i>=1; i--) 30 | 31 | 32 | #define INF (1<<28) 33 | #define PI 3.14159265358979323846264338327950 34 | #define pb(x) push_back(x) 35 | #define ppb pop_back 36 | #define all(x) x.begin(),x.end() 37 | #define mem(x,y) memset(x,y,sizeof(x)); 38 | #define eps 1e-9 39 | #define pii pair 40 | #define pmp make_pair 41 | 42 | 43 | #define sdi(x) scanf("%d",&x) 44 | #define sdii(x,y) scanf("%d%d",&x,&y) 45 | #define sds(x) scanf("%s",x) 46 | 47 | using namespace std; 48 | 49 | 50 | 51 | 52 | typedef long long i64; 53 | typedef unsigned long long ui64; 54 | 55 | #define READ(f) freopen(f, "r", stdin) 56 | #define WRITE(f) freopen(f, "w", stdout) 57 | 58 | 59 | 60 | 61 | 62 | i64 a[100004]; 63 | struct info 64 | { 65 | i64 add,sum; 66 | info(){add=0;sum=0;} 67 | info(i64 _sum,i64 _add) 68 | { 69 | add=_add; 70 | sum=_sum; 71 | } 72 | }m[400000]; 73 | 74 | void update(int node,int b,int e,int i,int j,i64 val) 75 | { 76 | if (i > e || j < b) return; 77 | i64 diff=(e-b+1); 78 | if (b >= i && e <= j) 79 | { 80 | m[node].sum+=diff*val; 81 | m[node].add+=val; 82 | return; 83 | 84 | } 85 | int Left=node<<1; 86 | int Right=(node<<1)+1; 87 | int mid=(b+e)>>1; 88 | update(Left,b,mid,i,j,val); 89 | update(Right,mid+1,e,i,j,val); 90 | m[node].sum=m[Left].sum+m[Right].sum+diff*m[node].add; 91 | } 92 | i64 query(int node,int b,int e,int i,int j,i64 carry) 93 | { 94 | 95 | if (i > e || j < b) return 0; 96 | if(b>=i and e<=j) 97 | { 98 | 99 | return m[node].sum+carry*(e-b+1); 100 | } 101 | 102 | int Left=node<<1; 103 | int Right=(node<<1)+1; 104 | int mid=(b+e)>>1; 105 | i64 p1 = query(Left, b,mid, i, j,carry+m[node].add); 106 | i64 p2 = query(Right, mid+1, e, i, j,carry+m[node].add); 107 | 108 | return p1+p2; 109 | } 110 | //vectorv[1000000]; 111 | int main() 112 | { 113 | 114 | //READ("in"); 115 | int t,kas=0; 116 | cin>>t; 117 | while(t--) 118 | { 119 | int n,q; 120 | sdii(n,q); 121 | 122 | mem(m,0); 123 | // printf("Case %d:\n",++kas); 124 | repl(i,q) 125 | { 126 | int com; 127 | sdi(com); 128 | if(com==0) 129 | { 130 | int idx,idx2,v; 131 | sdii(idx,idx2);sdi(v); 132 | 133 | //idx++; 134 | //idx2++; 135 | update(1,1,n,idx,idx2,v); 136 | 137 | } 138 | if(com==1) 139 | { 140 | int st,en; 141 | sdii(st,en); 142 | //st++; 143 | //en++; 144 | i64 get=query(1,1,n,st,en,0); 145 | printf("%lld\n",get); 146 | } 147 | 148 | } 149 | 150 | 151 | } 152 | 153 | 154 | return 0; 155 | } 156 | 157 | 158 | -------------------------------------------------------------------------------- /SPOJ/Paradox.py: -------------------------------------------------------------------------------- 1 | 2 | def dfs(u, cost): 3 | if u==0: return 4 | global flag 5 | if u in vis and vis[u]==1: 6 | if (cost-mem[u])%2==1: 7 | flag=0 8 | return 9 | mem[u]=cost 10 | vis[u]=1 11 | dfs(edg[u], cost+cst[u]) 12 | vis[u]=2 13 | 14 | while True: 15 | vis={} 16 | 17 | n=int(raw_input()) 18 | if n==0: break 19 | mem=[0 for i in range(0,n+2)] 20 | edg=[0 for i in range(0,n+2)] 21 | cst=[0 for i in range(0,n+2)] 22 | flag=1 23 | for i in range(1,n+1): 24 | s=raw_input().split(" ") 25 | if int(s[0])==i: 26 | if s[1]=="false": flag=0 27 | continue 28 | edg[i]=int(s[0]) 29 | if s[1]=="false": cst[i]=1 30 | 31 | for i in range(1,n+1): 32 | dfs(i,0) 33 | 34 | if flag==0: print "PARADOX" 35 | else: print "NOT PARADOX" 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /SPOJ/Sumsums.cpp: -------------------------------------------------------------------------------- 1 | //Programming Contest Template 2 | //Shafaet Ashraf 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #define stream istringstream 26 | #define rep(i,n) for(int i=0; i<(int)n; i++) 27 | #define repv(i,n) for(int i=n-1; i>=0; i--) 28 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 29 | #define replv(i,n) for(int i=n; i>=1; i--) 30 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 31 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 32 | #define sz(x) (int)x.size() 33 | #define inf (1061109567) 34 | #define pb(x) push_back(x) 35 | #define ppb pop_back 36 | #define all(x) x.begin(),x.end() 37 | #define mem(x,y) memset(x,y,sizeof(x)); 38 | #define eps 1e-9 39 | #define rev reverse 40 | #define pii pair 41 | #define pll pair 42 | #define pmp make_pair 43 | #define sdi(x) scanf("%d",&x) 44 | #define sdii(x,y) scanf("%d%d",&x,&y) 45 | #define sds(x) scanf("%s",x) 46 | #define pfi(x) printf("%d\n",x); 47 | #define uu first 48 | #define vv second 49 | using namespace std; 50 | template inline T sqr(T x){return x*x;} 51 | template inline T lcm(T a,T b) {if(a<0)return 52 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 53 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 54 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 55 | typedef long long i64; 56 | typedef unsigned long long ui64; 57 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 58 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 59 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 60 | b.end(), *q ) ) q++;}return oot; 61 | } 62 | //bool operator < ( const node& p ) const { return w < p.w; } 63 | #define on(n,pos) (n | (1LL<<(pos))) 64 | #define off(n,pos) n & ~(1LL<>n>>p; 123 | m.grid[0][0]=-1; 124 | m.grid[0][1]=1; 125 | m.grid[1][0]=0; 126 | m.grid[1][1]=n-1; 127 | 128 | m.power(p); 129 | //m.print(); 130 | i64 sum=0; 131 | for(int i=1;i<=n;i++) 132 | { 133 | cin>>a[i]; 134 | a[i]=a[i]%mod; 135 | sum+=a[i]; 136 | sum%=mod; 137 | } 138 | 139 | for(int i=1;i<=n;i++) 140 | { 141 | i64 ans=(a[i]*m.grid[0][0])%mod + (sum*m.grid[0][1])%mod; 142 | ans=(ans%mod+mod)%mod; 143 | cout< 4 | 5 | #define stream istringstream 6 | #define rep(i,n) for(int i=0; i<(int)n; i++) 7 | #define repv(i,n) for(int i=n-1; i>=0; i--) 8 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 9 | #define replv(i,n) for(int i=n; i>=1; i--) 10 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 11 | #define sz(x) (int)x.size() 12 | #define inf (1061109567) 13 | #define pb(x) push_back(x) 14 | #define ppb pop_back 15 | #define all(x) x.begin(),x.end() 16 | #define mem(x,y) memset(x,y,sizeof(x)); 17 | #define eps 1e-9 18 | #define rev reverse 19 | #define pii pair 20 | #define pmp make_pair 21 | #define uu first 22 | #define vv second 23 | using namespace std; 24 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 25 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 26 | typedef long long i64; 27 | typedef unsigned long long ui64; 28 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 29 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 30 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 31 | b.end(), *q ) ) q++;}return oot; 32 | } 33 | //bool operator < ( const node& p ) const { return w < p.w; } 34 | #define on(n,pos) (n | (1LL<<(pos))) 35 | #define off(n,pos) n & ~(1LL< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define stream istringstream 24 | #define rep(i,n) for(int i=0; i<(int)n; i++) 25 | #define repv(i,n) for(int i=n-1; i>=0; i--) 26 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 27 | #define replv(i,n) for(int i=n; i>=1; i--) 28 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 29 | #define SZ(x) (int)x.size() 30 | #define INF (1061109567) 31 | #define pb(x) push_back(x) 32 | #define ppb pop_back 33 | #define all(x) x.begin(),x.end() 34 | #define mem(x,y) memset(x,y,sizeof(x)); 35 | #define eps 1e-9 36 | #define pii pair 37 | #define pll pair 38 | #define pmp make_pair 39 | #define sdi(x) scanf("%d",&x) 40 | #define sdii(x,y) scanf("%d%d",&x,&y) 41 | #define sds(x) scanf("%s",x) 42 | #define pfi(x) printf("%d\n",x); 43 | #define uu first 44 | #define vv second 45 | using namespace std; 46 | template inline T sqr(T x){return x*x;} 47 | 48 | template inline T lcm(T a,T b) {if(a<0)return 49 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 50 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 51 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 52 | typedef long long i64; 53 | typedef unsigned long long ui64; 54 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 55 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 56 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 57 | b.end(), *q ) ) q++;}return oot; 58 | } 59 | #define on(n,pos) (n | (1<v) 103 | { 104 | int n=SZ(v); 105 | int ret=0; 106 | for(int i=0;itmp; 110 | int maxi=0; 111 | for(int j=0;j>n; 134 | vectorv; 135 | rep(i,n) 136 | { 137 | int x,y; 138 | sdii(x,y); 139 | v.pb(point(x,y)); 140 | 141 | } 142 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #define stream istringstream 25 | #define rep(i,n) for(__typeof(n) i=0; i<(n); i++) 26 | #define repl(i,n) for(__typeof(n) i=1; i<=(n); i++) 27 | #define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++) 28 | #define INF (1<<28) 29 | #define PI 3.14159265358979323846264338327950 30 | #define pb(x) push_back(x) 31 | #define ppb pop_back 32 | #define all(x) x.begin(),x.end() 33 | #define cclear(x,y) memset(x,y,sizeof(x)); 34 | #define mem(x,y) memset(x,y,sizeof(x)); 35 | #define eps 1e-9 36 | #define pii pair 37 | #define pll pair 38 | #define pmp make_pair 39 | 40 | 41 | 42 | using namespace std; 43 | 44 | template inline T gcd(T a,T b) {if(a<0)return 45 | gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);} 46 | template inline T lcm(T a,T b) {if(a<0)return 47 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));} 48 | template inline T sqr(T x){return x*x;} 49 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 50 | template bool inside(T a,T b,T c){ return (b>=a && b<=c);} 51 | typedef long long i64; 52 | typedef unsigned long long ui64; 53 | 54 | #define READ(f) freopen(f, "r", stdin) 55 | #define WRITE(f) freopen(f, "w", stdout) 56 | const i64 INF64 = (i64)1E16; 57 | 58 | 59 | int distsq2d(int x1,int y1,int x2,int y2){return sqr(x1-x2)+sqr(y1-y2);} 60 | double dist2d(double x1,double y1,double x2,double y2){return sqrt(sqr(x1-x2)+sqr(y1-y2));} 61 | double dist3d(double x1,double y1,double z1,double x2,double y2,double z2){ return sqrt(sqr(x1-x2)+sqr(y1-y2)+sqr(z1-z2));} 62 | 63 | i64 toInt64(string s){i64 r=0;istringstream sin(s);sin>>r;return r;} 64 | double LOG(i64 N,i64 B){ return (log10l(N))/(log10l(B)); } 65 | string itoa(long long a){if(a==0) return "0";string ret;for(long long i=a; i>0; i=i/10) ret.push_back((i%10)+48);reverse(ret.begin(),ret.end());return ret;} 66 | 67 | vector< string > token( string a, string b ) 68 | { 69 | const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 70 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), 71 | *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 72 | b.end(), *q ) ) q++;}return oot; 73 | } 74 | //bool operator < ( const node& p ) const { return w < p.w; } 75 | int isvowel(char s){s=tolower(s); if(s=='a' || s=='e' || s=='i' || s=='o' || s=='u')return 1; return 0;} 76 | int isupper(char s) {if(s>='A' and s<='Z') return 1; return 0;} 77 | int Set(int N,int pos){return N=N | (1<=0;i--) {bool x=check(N,i);cout<g[mx]; 86 | int deg[mx],pru[mx]; 87 | int main() 88 | { 89 | //READ("in"); 90 | int n=1; 91 | while(cin>>pru[n]) 92 | { 93 | 94 | deg[pru[n]]++; 95 | n++; 96 | } 97 | priority_queue< int, vector, greater >q; 98 | for(int i=1;i<=n;i++) 99 | { 100 | if(deg[i]==0) q.push(i); 101 | } 102 | for(int i=1;i<=n-1;i++) 103 | { 104 | int u=q.top(); q.pop(); 105 | int v=pru[i]; 106 | g[u].pb(v); 107 | g[v].pb(u); 108 | 109 | deg[v]--; 110 | if(deg[v]==0) q.push(v); 111 | 112 | } 113 | 114 | for(int i=1;i<=n;i++) 115 | { 116 | 117 | sort(all(g[i])); 118 | printf("%d:",i); 119 | for(int j=0;j<(int)g[i].size();j++) 120 | cout<<" "< 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #define stream istringstream 27 | #define rep(i,n) for(int i=0; i<(int)n; i++) 28 | #define repv(i,n) for(int i=n-1; i>=0; i--) 29 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 30 | #define replv(i,n) for(int i=n; i>=1; i--) 31 | 32 | #define SZ(x) (int)x.size() 33 | #define INF (1061109567) 34 | #define pb(x) push_back(x) 35 | #define ppb pop_back 36 | #define all(x) x.begin(),x.end() 37 | #define mem(x,y) memset(x,y,sizeof(x)); 38 | #define eps 1e-9 39 | #define pii pair 40 | #define pll pair 41 | #define pmp make_pair 42 | #define sdi(x) scanf("%d",&x) 43 | #define sdii(x,y) scanf("%d%d",&x,&y) 44 | #define sds(x) scanf("%s",x) 45 | #define pfi(x) printf("%d\n",x); 46 | #define uu first 47 | #define vv second 48 | using namespace std; 49 | template inline T sqr(T x){return x*x;} 50 | double dist(double x1,double y1,double x2,double y2){return sqrt(sqr(x1-x2)+sqr(y1-y2));} 51 | template inline T lcm(T a,T b) {if(a<0)return 52 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 53 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 54 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 55 | typedef long long i64; 56 | typedef unsigned long long ui64; 57 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 58 | vector< string > token(char *a,char *b) {vectorv; char *p=strtok(a," "); while(p) {v.pb(p);p=strtok(NULL," ");} return v;} 59 | vectortoken(string a,char *b){char temp[100002]; strcpy(temp,a.c_str()); return token(temp,b); } 60 | #define on(n,pos) n=(n | (1<g[mx]; 73 | int vis[mx],par[mx]; 74 | int src,maxi; 75 | void dfs(int from,int u,int dep) 76 | { 77 | vis[u]=1; 78 | if(dep+a[u]>maxi) 79 | { 80 | maxi=dep+a[u]; 81 | src=u; 82 | } 83 | rep(i,SZ(g[u])) 84 | { 85 | int v=g[u][i].first; 86 | int w=g[u][i].second; 87 | if(v==from) continue; 88 | vis[v]=1; 89 | dfs(u,v,dep+w+a[u]); 90 | } 91 | } 92 | void dfs2(int from,int u,int des) 93 | { 94 | if(u==des) return; 95 | rep(i,SZ(g[u])) 96 | { 97 | int v=g[u][i].first; 98 | if(v==from) continue; 99 | par[v]=u; 100 | dfs2(u,v,des); 101 | } 102 | } 103 | int main(){ 104 | 105 | 106 | //READ("in"); 107 | int n,e; 108 | cin>>n>>e; 109 | rep(i,n) 110 | sdi(a[i]); 111 | 112 | rep(i,e) 113 | { 114 | int u,v,w; 115 | sdii(u,v); u--;v--; 116 | sdi(w); 117 | g[u].pb(pii(v,w)); 118 | g[v].pb(pii(u,w)); 119 | } 120 | 121 | int gm=0,ns,nd; 122 | rep(i,n) 123 | { 124 | if(!vis[i]) 125 | { 126 | 127 | maxi=0; 128 | dfs(-1,i,0); 129 | maxi=0; 130 | int d=src; 131 | dfs(-1,src,0); 132 | 133 | if(maxi>gm) 134 | { 135 | gm=maxi; 136 | ns=src; 137 | nd=d; 138 | } 139 | } 140 | } 141 | 142 | dfs2(-1,ns,nd); 143 | vectorout; 144 | while(nd!=ns) 145 | { 146 | out.pb(nd+1); 147 | nd=par[nd]; 148 | } 149 | out.pb(nd+1); 150 | 151 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define stream istringstream 24 | #define rep(i,n) for(int i=0; i<(int)n; i++) 25 | #define repv(i,n) for(int i=n-1; i>=0; i--) 26 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 27 | #define replv(i,n) for(int i=n; i>=1; i--) 28 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 29 | #define SZ(x) (int)x.size() 30 | #define INF (1061109567) 31 | #define pb(x) push_back(x) 32 | #define ppb pop_back 33 | #define all(x) x.begin(),x.end() 34 | #define mem(x,y) memset(x,y,sizeof(x)); 35 | #define eps 1e-9 36 | #define pii pair 37 | #define pll pair 38 | #define pmp make_pair 39 | #define sdi(x) scanf("%d",&x) 40 | #define sdii(x,y) scanf("%d%d",&x,&y) 41 | #define sds(x) scanf("%s",x) 42 | #define pfi(x) printf("%d\n",x); 43 | #define uu first 44 | #define vv second 45 | using namespace std; 46 | template inline T sqr(T x){return x*x;} 47 | 48 | template inline T lcm(T a,T b) {if(a<0)return 49 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 50 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 51 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 52 | typedef long long i64; 53 | typedef unsigned long long ui64; 54 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 55 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 56 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 57 | b.end(), *q ) ) q++;}return oot; 58 | } 59 | #define on(n,pos) (n | (1<>a>>b; 113 | 114 | cout< 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #define stream istringstream 29 | #define rep(i,n) for(int i=0; i<(int)n; i++) 30 | #define repv(i,n) for(int i=n-1; i>=0; i--) 31 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 32 | #define replv(i,n) for(int i=n; i>=1; i--) 33 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 34 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 35 | #define sz(x) (int)x.size() 36 | #define inf (1061109567) 37 | #define pb(x) push_back(x) 38 | #define ppb pop_back 39 | #define all(x) x.begin(),x.end() 40 | #define mem(x,y) memset(x,y,sizeof(x)); 41 | #define eps 1e-9 42 | #define rev reverse 43 | #define pii pair 44 | #define pll pair 45 | #define pmp make_pair 46 | #define sdi(x) scanf("%d",&x) 47 | #define sdii(x,y) scanf("%d%d",&x,&y) 48 | #define sds(x) scanf("%s",x) 49 | #define pfi(x) printf("%d\n",x); 50 | #define uu first 51 | #define vv second 52 | using namespace std; 53 | template inline T sqr(T x){return x*x;} 54 | template inline T lcm(T a,T b) {if(a<0)return 55 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 56 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 57 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 58 | typedef long long i64; 59 | typedef unsigned long long ui64; 60 | 61 | bool eq(long double a,long double b){return fabs(a-b)b) return 0; 71 | return s[b]-s[a-1]; 72 | } 73 | int call(int a,int b) 74 | { 75 | if(a>b) return 0; 76 | int ans=1<<28; 77 | for(int m=a;m<=b;m++) 78 | { 79 | int ret=call(a,m-1)+call(m+1,b)+cost(a,m-1)+cost(m+1,b); 80 | ans=min(ans,ret); 81 | } 82 | return ans; 83 | } 84 | int dp[mx][mx],mid[mx][mx]; 85 | int iter(int n) 86 | { 87 | for(int s=1;s<=n;s++) 88 | { 89 | for(int a=1;a+s-1<=n;a++) 90 | { 91 | int b=a+s-1; 92 | if(a==b) 93 | { 94 | dp[a][b]=0; 95 | mid[a][b]=a; 96 | continue; 97 | } 98 | 99 | dp[a][b]=1<<28; 100 | for(int m=mid[a][b-1];m<=mid[a+1][b];m++) 101 | { 102 | int ret=dp[a][m-1]+dp[m+1][b]+cost(a,m-1)+cost(m+1,b); 103 | if(ret>n) 120 | { 121 | repl(i,n)cin>>f[i],s[i]=s[i-1]+f[i]; 122 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define stream istringstream 24 | #define rep(i,n) for(int i=0; i<(int)n; i++) 25 | #define repv(i,n) for(int i=n-1; i>=0; i--) 26 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 27 | #define replv(i,n) for(int i=n; i>=1; i--) 28 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 29 | #define SZ(x) (int)x.size() 30 | #define INF (1061109567) 31 | #define pb(x) push_back(x) 32 | #define ppb pop_back 33 | #define all(x) x.begin(),x.end() 34 | #define mem(x,y) memset(x,y,sizeof(x)); 35 | #define eps 1e-9 36 | #define pii pair 37 | #define pll pair 38 | #define pmp make_pair 39 | #define sdi(x) scanf("%d",&x) 40 | #define sdii(x,y) scanf("%d%d",&x,&y) 41 | #define sds(x) scanf("%s",x) 42 | #define pfi(x) printf("%d\n",x); 43 | #define uu first 44 | #define vv second 45 | using namespace std; 46 | template inline T sqr(T x){return x*x;} 47 | 48 | template inline T lcm(T a,T b) {if(a<0)return 49 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 50 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 51 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 52 | typedef long long i64; 53 | typedef unsigned long long ui64; 54 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 55 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 56 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 57 | b.end(), *q ) ) q++;}return oot; 58 | } 59 | #define on(n,pos) (n | (1<>n>>e>>bh>>bo>>yh>>m) 83 | { 84 | repl(i,n) 85 | { 86 | repl(j,n) 87 | { 88 | w[i][j]=1<<28; 89 | w2[i][j]=1<<28; 90 | } 91 | w[i][i]=0; 92 | w2[i][i]=0; 93 | } 94 | repl(i,e) 95 | { 96 | int u,v,c; 97 | sdii(u,v); 98 | sdi(c); 99 | w[u][v]=w[v][u]=min(w[u][v],c); 100 | w2[u][v]=w2[v][u]=min(w2[u][v],c); 101 | } 102 | repl(k,n) 103 | repl(i,n) 104 | repl(j,n) 105 | w[i][j]=min(w[i][j],w[i][k]+w[k][j]); 106 | 107 | int baad[102]={0}; 108 | repl(k,n) 109 | { 110 | if(w[bh][k]+w[k][bo]==w[bh][bo]) 111 | { 112 | baad[k]=1; 113 | 114 | } 115 | } 116 | 117 | 118 | 119 | repl(k,n) 120 | repl(i,n) 121 | repl(j,n) 122 | if(!baad[i] and !baad[j] and !baad[k]){ 123 | 124 | w2[i][j]=min(w2[i][j],w2[i][k]+w2[k][j]); 125 | } 126 | if(w2[yh][m]==1<<28 or baad[yh] or baad[m]) puts("MISSION IMPOSSIBLE."); 127 | else 128 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define stream istringstream 24 | #define rep(i,n) for(int i=0; i<(int)n; i++) 25 | #define repv(i,n) for(int i=n-1; i>=0; i--) 26 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 27 | #define replv(i,n) for(int i=n; i>=1; i--) 28 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 29 | #define SZ(x) (int)x.size() 30 | #define INF (1061109567) 31 | #define pb(x) push_back(x) 32 | #define ppb pop_back 33 | #define all(x) x.begin(),x.end() 34 | #define mem(x,y) memset(x,y,sizeof(x)); 35 | #define eps 1e-9 36 | #define pii pair 37 | #define pll pair 38 | #define pmp make_pair 39 | #define sdi(x) scanf("%d",&x) 40 | #define sdii(x,y) scanf("%d%d",&x,&y) 41 | #define sds(x) scanf("%s",x) 42 | #define pfi(x) printf("%d\n",x); 43 | #define uu first 44 | #define vv second 45 | using namespace std; 46 | template inline T sqr(T x){return x*x;} 47 | 48 | template inline T lcm(T a,T b) {if(a<0)return 49 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 50 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 51 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 52 | typedef long long i64; 53 | typedef unsigned long long ui64; 54 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 55 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 56 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 57 | b.end(), *q ) ) q++;}return oot; 58 | } 59 | #define on(n,pos) (n | (1<>test; 79 | 80 | while(test--) 81 | { 82 | int t[mx][mx]; 83 | int l[mx][mx]; 84 | int n,e; 85 | sdii(n,e); 86 | repl(i,n) 87 | repl(j,n) 88 | if(i==j)t[i][j]=l[i][j]=0; 89 | else t[i][j]=l[i][j]=1<<28; 90 | 91 | rep(i,e) 92 | { 93 | int u,v,wt,wl; 94 | sdii(u,v); 95 | sdii(wt,wl); 96 | if(wt 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #define stream istringstream 27 | #define rep(i,n) for(int i=0; i<(int)n; i++) 28 | #define repv(i,n) for(int i=n-1; i>=0; i--) 29 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 30 | #define replv(i,n) for(int i=n; i>=1; i--) 31 | 32 | #define SZ(x) (int)x.size() 33 | #define INF (1061109567) 34 | #define pb(x) push_back(x) 35 | #define ppb pop_back 36 | #define all(x) x.begin(),x.end() 37 | #define mem(x,y) memset(x,y,sizeof(x)); 38 | #define eps 1e-9 39 | #define pii pair 40 | #define pll pair 41 | #define pmp make_pair 42 | #define sdi(x) scanf("%d",&x) 43 | #define sdii(x,y) scanf("%d%d",&x,&y) 44 | #define sds(x) scanf("%s",x) 45 | #define pfi(x) printf("%d\n",x); 46 | #define uu first 47 | #define vv second 48 | using namespace std; 49 | template inline T sqr(T x){return x*x;} 50 | double dist(double x1,double y1,double x2,double y2){return sqrt(sqr(x1-x2)+sqr(y1-y2));} 51 | template inline T lcm(T a,T b) {if(a<0)return 52 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 53 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 54 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 55 | typedef long long i64; 56 | typedef unsigned long long ui64; 57 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 58 | vector< string > token(char *a,char *b) {vectorv; char *p=strtok(a," "); while(p) {v.pb(p);p=strtok(NULL," ");} return v;} 59 | vectortoken(string a,char *b){char temp[100002]; strcpy(temp,a.c_str()); return token(temp,b); } 60 | #define on(n,pos) n=(n | (1<st[1102]; 74 | void clr(int i) 75 | { 76 | while(SZ(st[i]))st[i].pop(); 77 | } 78 | int main(){ 79 | //READ("in"); 80 | string s; 81 | int ks=0; 82 | while(cin>>s and s!="end") 83 | { 84 | repl(i,1100) 85 | clr(i); 86 | int c=0; 87 | rep(i,SZ(s)) 88 | { 89 | int flag=0; 90 | for(int j=1;j<=1001;j++) 91 | { 92 | if(SZ(st[j]) and s[i]<=st[j].top()) { 93 | flag=1; st[j].push(s[i]); break; 94 | } 95 | } 96 | if(flag==0) 97 | { 98 | for(int j=1;j<=1001;j++) 99 | { 100 | if(!SZ(st[j])){flag=1; st[j].push(s[i]); break;} 101 | } 102 | } 103 | } 104 | 105 | for(int j=1;j<=1001;j++) 106 | { 107 | c+=(SZ(st[j])>0); 108 | } 109 | printf("Case %d: %d\n",++ks,c); 110 | } 111 | 112 | return 0; 113 | } 114 | 115 | 116 | -------------------------------------------------------------------------------- /UVA/11031 - Looking for a Subset.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * UVA 11031 - Looking for a Subset 3 | * LIS + printing path(leftmost) 4 | * O(nlogk) 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #define stream istringstream 30 | #define rep(i,n) for(int i=0; i<(int)n; i++) 31 | #define repv(i,n) for(int i=n-1; i>=0; i--) 32 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 33 | #define replv(i,n) for(int i=n; i>=1; i--) 34 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 35 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 36 | #define sz(x) (int)x.size() 37 | #define inf (1061109567) 38 | #define pb(x) push_back(x) 39 | #define ppb pop_back 40 | #define all(x) x.begin(),x.end() 41 | #define mem(x,y) memset(x,y,sizeof(x)); 42 | #define eps 1e-9 43 | #define rev reverse 44 | #define pii pair 45 | #define pll pair 46 | #define pmp make_pair 47 | #define sdi(x) scanf("%d",&x) 48 | #define sdii(x,y) scanf("%d%d",&x,&y) 49 | #define sds(x) scanf("%s",x) 50 | #define pfi(x) printf("%d\n",x); 51 | #define uu first 52 | #define vv second 53 | using namespace std; 54 | template inline T sqr(T x){return x*x;} 55 | template inline T lcm(T a,T b) {if(a<0)return 56 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 57 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 58 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 59 | typedef long long i64; 60 | typedef unsigned long long ui64; 61 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 62 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 63 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 64 | b.end(), *q ) ) q++;}return oot; 65 | } 66 | //bool operator < ( const node& p ) const { return w < p.w; } 67 | #define on(n,pos) (n | (1<<(pos))) 68 | #define off(n,pos) n & ~(1<v; 85 | v.pb(-inf); 86 | for(int i=0;i findsub(int n,int len) //Returns LEFTMOST Increasing Subsequence of Length "len" 106 | { 107 | vectorres; 108 | rep(i,n) 109 | { 110 | if(lis[i]>=len and (!sz(res) or num[i]>res.back())) 111 | res.pb(num[i]),len--; 112 | if(!len) break; 113 | } 114 | return res; 115 | } 116 | int main() 117 | { 118 | READ("in"); 119 | freopen("out1","w",stdout); 120 | int n,q,ks=0; 121 | while(cin>>n>>q) 122 | { 123 | if(!n and !q) break; 124 | rep(i,n) sdi(num[i]); 125 | LIS(n,num); 126 | printf("Set %d:\n",++ks); 127 | repl(i,q) 128 | { 129 | 130 | int lim; 131 | sdi(lim); 132 | vectorres=findsub(n,lim); 133 | printf(" Subset %d:\n",i); 134 | if(lim>sz(res)) puts(" Impossible"); 135 | else 136 | { 137 | 138 | printf(" "); 139 | rep(i,lim)printf(" %d",res[i]); 140 | puts(""); 141 | } 142 | } 143 | } 144 | 145 | 146 | } 147 | -------------------------------------------------------------------------------- /UVA/11119(Chemical Attraction).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #define stream istringstream 22 | #define rep(i,n) for(__typeof(n) i=0; i<(n); i++) 23 | #define repl(i,n) for(__typeof(n) i=1; i<=(n); i++) 24 | #define INF (1<<28) 25 | #define PI 3.14159265358979323846264338327950 26 | #define pb(x) push_back(x) 27 | #define ppb pop_back 28 | #define all(x) x.begin(),x.end() 29 | #define mem(x,y) memset(x,y,sizeof(x)); 30 | #define eps 1e-9 31 | #define pii pair 32 | #define pll pair 33 | #define pmp make_pair 34 | #define sz(s) ((int)(s.size())) 35 | 36 | 37 | using namespace std; 38 | template inline T gcd(T a,T b) {if(a<0)return 39 | gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);} 40 | template inline T lcm(T a,T b) {if(a<0)return 41 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));} 42 | template inline T sqr(T x){return x*x;} 43 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 44 | template bool inside(T a,T b,T c){ return (b>=a && b<=c);} 45 | typedef long long i64; 46 | typedef unsigned long long ui64; 47 | 48 | #define READ(f) freopen(f, "r", stdin) 49 | #define WRITE(f) freopen(f, "w", stdout) 50 | 51 | int man[202][202],woman[202][202]; 52 | mapmp; 53 | string ulta[202]; 54 | 55 | void solve(vector&mlist,vector&wlist,int n) 56 | { 57 | sort(all(mlist)); 58 | sort(all(wlist)); 59 | 60 | int Left[202]={0},Right[202]={0}; 61 | mem(Left,-1); 62 | mem(Right,-1); 63 | int prop[202][202]={{0}}; 64 | 65 | while(1) 66 | { 67 | int rem=0; 68 | vectorlist[102]; 69 | rep(i,mlist.size()) 70 | { 71 | if(Left[i]>=0) continue; 72 | rem++; 73 | int mx=0,choice=0; 74 | rep(j,wlist.size()) 75 | { 76 | if(prop[i][j]) continue; 77 | int u=mlist[i]; 78 | int v=wlist[j]; 79 | if(man[u][v]>mx){ 80 | 81 | mx=man[u][v]; 82 | choice=j; 83 | } 84 | } 85 | 86 | list[choice].pb(i); 87 | prop[i][choice]=1; 88 | 89 | } 90 | if(!rem) break; 91 | rep(i,wlist.size()) 92 | { 93 | int mx=0; 94 | if(Right[i]!=-1)list[i].pb(Right[i]); 95 | rep(j,list[i].size()) 96 | { 97 | 98 | int u=wlist[i]; 99 | int v=mlist[list[i][j]]; 100 | if(woman[u][v]>mx) 101 | { 102 | mx=woman[u][v]; 103 | if(Right[i]!=-1) Left[Right[i]]=-1; 104 | Right[i]=list[i][j]; 105 | Left[list[i][j]]=i; 106 | } 107 | } 108 | } 109 | 110 | } 111 | rep(i,mlist.size()) 112 | { 113 | if(i) printf(" "); 114 | cout<>n and n) 125 | { 126 | 127 | mp.clear(); 128 | for(int i=1;i<=n;i++) 129 | { 130 | string s; 131 | cin>>s; 132 | ulta[i]=s; 133 | mp[s]=i; 134 | } 135 | int m; 136 | cin>>m; 137 | for(int i=1;i<=m;i++) 138 | { 139 | string s; 140 | cin>>s; 141 | mp[s]=i+n; 142 | ulta[i+n]=s; 143 | } 144 | 145 | for(int i=1;i<=n;i++) 146 | for(int j=1;j<=m;j++) 147 | scanf("%d",&man[i][j+n]); 148 | 149 | for(int i=1;i<=m;i++) 150 | for(int j=1;j<=n;j++) 151 | scanf("%d",&woman[i+n][j]); 152 | 153 | int q,qu=0; 154 | ++kas; 155 | while(cin>>q and q) 156 | { 157 | 158 | 159 | vectorvm,vw; 160 | for(int i=1;i<=q;i++) 161 | { 162 | string s; 163 | cin>>s; 164 | string s1,s2; 165 | s1=s.substr(0,2); 166 | s2=s.substr(2,2); 167 | vm.pb(mp[s1]); 168 | vw.pb(mp[s2]); 169 | 170 | } 171 | printf("Scenario %d, Mixture %d:\n",kas,++qu); 172 | solve(vm,vw,n); 173 | puts(""); 174 | 175 | } 176 | } 177 | 178 | 179 | } 180 | 181 | 182 | -------------------------------------------------------------------------------- /UVA/12319(Edgetown's Traffic jam).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define stream istringstream 24 | #define rep(i,n) for(int i=0; i<(int)n; i++) 25 | #define repv(i,n) for(int i=n-1; i>=0; i--) 26 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 27 | #define replv(i,n) for(int i=n; i>=1; i--) 28 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 29 | #define SZ(x) (int)x.size() 30 | #define INF (1061109567) 31 | #define pb(x) push_back(x) 32 | #define ppb pop_back 33 | #define all(x) x.begin(),x.end() 34 | #define mem(x,y) memset(x,y,sizeof(x)); 35 | #define eps 1e-9 36 | #define pii pair 37 | #define pll pair 38 | #define pmp make_pair 39 | #define sdi(x) scanf("%d",&x) 40 | #define sdii(x,y) scanf("%d%d",&x,&y) 41 | #define sds(x) scanf("%s",x) 42 | #define pfi(x) printf("%d\n",x); 43 | #define uu first 44 | #define vv second 45 | using namespace std; 46 | template inline T sqr(T x){return x*x;} 47 | 48 | template inline T lcm(T a,T b) {if(a<0)return 49 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 50 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 51 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 52 | typedef long long i64; 53 | typedef unsigned long long ui64; 54 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 55 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 56 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 57 | b.end(), *q ) ) q++;}return oot; 58 | } 59 | #define on(n,pos) (n | (1<>v) 102 | { 103 | w[u][v]=1; 104 | } 105 | } 106 | 107 | 108 | 109 | 110 | repl(i,n) 111 | { 112 | int u=i,v; 113 | 114 | 115 | string s; 116 | getline(cin,s); 117 | istringstream sin(s); 118 | 119 | while(sin>>v) 120 | { 121 | w2[u][v]=1; 122 | } 123 | } 124 | int A,B; 125 | cin>>A>>B; 126 | repl(k,n) 127 | repl(i,n) 128 | repl(j,n) 129 | w[i][j]=min(w[i][j],w[i][k]+w[k][j]); 130 | 131 | 132 | 133 | 134 | repl(k,n) 135 | repl(i,n) 136 | repl(j,n) 137 | w2[i][j]=min(w2[i][j],w2[i][k]+w2[k][j]); 138 | int flag=1; 139 | repl(i,n) 140 | repl(j,n) 141 | { 142 | int nw=w2[i][j]; 143 | int x=w[i][j]; 144 | if(nw>A*x+B) { 145 | 146 | flag=0; 147 | } 148 | } 149 | puts(flag==0?"No":"Yes"); 150 | 151 | } 152 | return 0; 153 | } 154 | -------------------------------------------------------------------------------- /UVA/1424(Salesman).cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Shafaetsplanet.com 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #define stream istringstream 27 | #define rep(i,n) for(int i=0; i<(int)n; i++) 28 | #define repv(i,n) for(int i=n-1; i>=0; i--) 29 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 30 | #define replv(i,n) for(int i=n; i>=1; i--) 31 | 32 | #define SZ(x) (int)x.size() 33 | #define INF (1061109567) 34 | #define pb(x) push_back(x) 35 | #define ppb pop_back 36 | #define all(x) x.begin(),x.end() 37 | #define mem(x,y) memset(x,y,sizeof(x)); 38 | #define eps 1e-9 39 | #define pii pair 40 | #define pll pair 41 | #define pmp make_pair 42 | #define sdi(x) scanf("%d",&x) 43 | #define sdii(x,y) scanf("%d%d",&x,&y) 44 | #define sds(x) scanf("%s",x) 45 | #define pfi(x) printf("%d\n",x); 46 | #define uu first 47 | #define vv second 48 | using namespace std; 49 | template inline T sqr(T x){return x*x;} 50 | double dist(double x1,double y1,double x2,double y2){return sqrt(sqr(x1-x2)+sqr(y1-y2));} 51 | template inline T lcm(T a,T b) {if(a<0)return 52 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 53 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 54 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 55 | typedef long long i64; 56 | typedef unsigned long long ui64; 57 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 58 | vector< string > token(char *a,char *b) {vectorv; char *p=strtok(a," "); while(p) {v.pb(p);p=strtok(NULL," ");} return v;} 59 | vectortoken(string a,char *b){char temp[100002]; strcpy(temp,a.c_str()); return token(temp,b); } 60 | #define on(n,pos) n=(n | (1<g[mx]; 73 | int dp[mx][mx]; 74 | int call(int u,int idx) 75 | { 76 | if(idx==c+1) return 0; 77 | if(dp[u][idx]!=-1) return dp[u][idx]; 78 | int ret=(u!=a[idx])+call(u,idx+1); 79 | rep(i,SZ(g[u])) 80 | { 81 | int v=g[u][i]; 82 | ret=min(ret,(v!=a[idx])+call(v,idx+1)); 83 | } 84 | return dp[u][idx]=ret; 85 | 86 | } 87 | int main(){ 88 | //READ("in"); 89 | int t; 90 | cin>>t; 91 | while(t--) 92 | { 93 | mem(dp,-1); 94 | int n,e; 95 | c=0; 96 | sdii(n,e); 97 | rep(i,n+3)g[i].clear(); 98 | 99 | //repl(i,n)g[i].pb(i) 100 | rep(i,e) 101 | { 102 | int u,v; 103 | sdii(u,v); 104 | g[u].pb(v); 105 | g[v].pb(u); 106 | } 107 | sdi(c); 108 | repl(i,c) 109 | sdi(a[i]); 110 | 111 | int ans=1<<29; 112 | repl(i,n) 113 | { 114 | int ret=(i!=a[1])+call(i,2); 115 | ans=min(ans,ret); 116 | } 117 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define stream istringstream 24 | #define rep(i,n) for(int i=0; i<(int)n; i++) 25 | #define repv(i,n) for(int i=n-1; i>=0; i--) 26 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 27 | #define replv(i,n) for(int i=n; i>=1; i--) 28 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 29 | #define SZ(x) (int)x.size() 30 | #define INF (1061109567) 31 | #define pb(x) push_back(x) 32 | #define ppb pop_back 33 | #define all(x) x.begin(),x.end() 34 | #define mem(x,y) memset(x,y,sizeof(x)); 35 | #define eps 1e-9 36 | #define pii pair 37 | #define pll pair 38 | #define pmp make_pair 39 | #define sdi(x) scanf("%d",&x) 40 | #define sdii(x,y) scanf("%d%d",&x,&y) 41 | #define sds(x) scanf("%s",x) 42 | #define pfi(x) printf("%d\n",x); 43 | #define uu first 44 | #define vv second 45 | using namespace std; 46 | template inline T sqr(T x){return x*x;} 47 | 48 | template inline T lcm(T a,T b) {if(a<0)return 49 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 50 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 51 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 52 | typedef long long i64; 53 | typedef unsigned long long ui64; 54 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 55 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 56 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 57 | b.end(), *q ) ) q++;}return oot; 58 | } 59 | #define on(n,pos) (n | (1<last) 116 | { 117 | at=1; 118 | last=t; 119 | if(du<=dv) x=v; 120 | else x=u; 121 | } 122 | } 123 | else 124 | { 125 | 126 | t=min(du,dv)+abs(du-dv)+(uv-abs(du-dv))/2.00; 127 | if(t>last) 128 | { 129 | at=0; 130 | last=t; 131 | x=u; y=v; 132 | } 133 | } 134 | 135 | //cout<0) 11 | { 12 | sum+=tree[idx]; 13 | idx -= idx & (-idx); 14 | } 15 | return sum; 16 | 17 | } 18 | void update(int idx, int x, int n) //n is the size of the array, x is the number to add 19 | { 20 | while(idx<=n) 21 | { 22 | tree[idx]+=x; 23 | idx += idx & (-idx); 24 | } 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Useful C++ Libraries/Matrix Exponentiation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Usage: fill the grid array using desired value. 3 | * Use the power function for get exponent in dim^3*log(n) complexity 4 | * Find my solution of spoj sumsums in this repo for example usage. 5 | */ 6 | 7 | i64 fixmod(i64 a,i64 m) //For handling mod of negative numbers 8 | { 9 | return (a%m + m)%m; 10 | } 11 | struct matrix{ 12 | #define dim 2 13 | #define i64 long long 14 | int n,mod; 15 | i64 grid[dim][dim]; 16 | matrix(int a,int b) {n=a,mod=b; mem(grid,0);} 17 | 18 | matrix operator * (const matrix &b) { 19 | matrix ret(n,mod); 20 | rep(i,n) rep(j,n) rep(k,n) 21 | { 22 | i64 a=fixmod(grid[i][k]*b.grid[k][j],mod); 23 | ret.grid[i][j]= fixmod(ret.grid[i][j]+ a,mod); 24 | } 25 | return ret; 26 | }; 27 | 28 | void power(int p) 29 | { 30 | if(p<=1) return ; 31 | if(p%2){ 32 | matrix temp(n,mod); 33 | rep(i,n) rep(j,n) rep(k,n) temp.grid[i][j]=grid[i][j]; 34 | power(p-1); 35 | (*this)=(*this)*temp; 36 | } 37 | else { (*this)=(*this)*(*this); power(p/2); } 38 | } 39 | 40 | void print() 41 | { 42 | rep(i,n) { rep(j,n) cout< 3 | using namespace std; 4 | 5 | typedef long long i64; 6 | 7 | //Returns the index of the first match 8 | //Complexity O(n+m), this is unsafe because it doesn't check for collisons 9 | 10 | i64 Hash(const string &s, int m, i64 B, i64 M){ 11 | i64 h = 0 , power = 1; 12 | for(int i = m-1;i>=0;i--){ 13 | h = h + (s[i] * power) % M; 14 | h = h % M; 15 | power = (power * B)%M; 16 | } 17 | return h; 18 | } 19 | int match(const string &text, const string &pattern) { 20 | int n = text.size(); 21 | int m = pattern.size(); 22 | if(n < m)return -1; 23 | if(m == 0 or n == 0) 24 | return -1; 25 | 26 | i64 B = 347, M = 1000000000+7; 27 | 28 | //Calculate B^(m-1) 29 | i64 power = 1; 30 | for(int i=1;i<=m-1;i++) 31 | power = (power * B) % M; 32 | 33 | //Find hash value of first m characters of text 34 | //Find hash value of pattern 35 | i64 hash_text = Hash(text, m, B, M); 36 | i64 hash_pattern = Hash(pattern, m, B, M); 37 | 38 | if(hash_text == hash_pattern){ //returns the index of the match 39 | return 0; 40 | //We should've checked the substrings character by character here as hash collision might happen 41 | } 42 | 43 | for(int i=m;i 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #define stream istringstream 25 | #define rep(i,n) for(int i=0; i<(int)n; i++) 26 | #define repv(i,n) for(int i=n-1; i>=0; i--) 27 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 28 | #define replv(i,n) for(int i=n; i>=1; i--) 29 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 30 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 31 | #define sz(x) (int)x.size() 32 | #define inf (1061109567) 33 | #define pb(x) push_back(x) 34 | #define ppb pop_back 35 | #define all(x) x.begin(),x.end() 36 | #define mem(x,y) memset(x,y,sizeof(x)); 37 | #define eps 1e-9 38 | #define rev reverse 39 | #define pii pair 40 | #define pll pair 41 | #define pmp make_pair 42 | #define sdi(x) scanf("%d",&x) 43 | #define sdii(x,y) scanf("%d%d",&x,&y) 44 | #define sds(x) scanf("%s",x) 45 | #define pfi(x) printf("%d\n",x); 46 | #define uu first 47 | #define vv second 48 | using namespace std; 49 | template inline T sqr(T x){return x*x;} 50 | template inline T lcm(T a,T b) {if(a<0)return 51 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 52 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 53 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 54 | typedef long long i64; 55 | typedef unsigned long long ui64; 56 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 57 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 58 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 59 | b.end(), *q ) ) q++;}return oot; 60 | } 61 | //bool operator < ( const node& p ) const { return w < p.w; } 62 | #define on(n,pos) (n | (1< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define stream istringstream 24 | #define rep(i,n) for(int i=0; i<(int)n; i++) 25 | #define repv(i,n) for(int i=n-1; i>=0; i--) 26 | #define repl(i,n) for(int i=1; i<=(int)n; i++) 27 | #define replv(i,n) for(int i=n; i>=1; i--) 28 | #define FOR(i,a,b) for(int i=(int)a;i<=(int)b;i++) 29 | #define foreach(i,n) for(__typeof((n).begin())i =(n).begin();i!=(n).end();i++) 30 | #define sz(x) (int)x.size() 31 | #define inf (1061109567) 32 | #define pb(x) push_back(x) 33 | #define ppb pop_back 34 | #define all(x) x.begin(),x.end() 35 | #define mem(x,y) memset(x,y,sizeof(x)); 36 | #define eps 1e-9 37 | #define rev reverse 38 | #define pii pair 39 | #define pll pair 40 | #define pmp make_pair 41 | #define sdi(x) scanf("%d",&x) 42 | #define sdii(x,y) scanf("%d%d",&x,&y) 43 | #define sds(x) scanf("%s",x) 44 | #define pfi(x) printf("%d\n",x); 45 | #define uu first 46 | #define vv second 47 | using namespace std; 48 | template inline T sqr(T x){return x*x;} 49 | template inline T lcm(T a,T b) {if(a<0)return 50 | lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/__gcd(a,b));} 51 | template T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); } 52 | template string itoa(T a){if(!a) return "0";string ret;for(T i=a; i>0; i=i/10) ret.pb((i%10)+48);reverse(all(ret));return ret;} 53 | typedef long long i64; 54 | typedef unsigned long long ui64; 55 | double log(double N,double B){ return (log10l(N))/(log10l(B)); } 56 | vector< string > token( string a, string b ) {const char *q = a.c_str();while( count( b.begin(), b.end(), *q ) ) q++;vector< string > 57 | oot;while( *q ) {const char *e = q;while( *e && !count( b.begin(), b.end(), *e ) ) e++;oot.push_back( string( q, e ) );q = e;while( count( b.begin(), 58 | b.end(), *q ) ) q++;}return oot; 59 | } 60 | //bool operator < ( const node& p ) const { return w < p.w; } 61 | #define on(n,pos) (n | (1<<(pos))) 62 | #define off(n,pos) n & ~(1<ms; 118 | int f1[mx+2]={0},f2[mx+2]={0}; 119 | for(int j=0;j