#include <bits/stdc++.h> using namespace std; int main() { srand(2); int a[201][201]; for(int i = 1; i <= 200; i++) for(int j = 1; j <= 200; j++) a[i][j] = 0; vector<pair<int, int> > v; //---------------- SET UP ADJACENCY MATRIX ---------------- for(int i = 1; i <= 200; i++){ string s, temp = ""; getline(cin, s); for(int j = 0; j < s.length(); j++){ if (s[j] != 9){ temp += s[j]; } else { stringstream container(temp); int k; container >> k; temp = ""; if(i > k && k != 0) { a[i][k] = 1; v.push_back(make_pair(i, k)); } } } } //---------------- PROCESS PAIRS WHEN > 1 PAIR REMAINING ---------------- while(v.size() != 1){ int r = rand() % v.size(); int f = v[r].first, s = v[r].second; v[r] = v[v.size()-1]; v.pop_back(); //---------------- PROCESS VERTICES LARGER THAN THE ONE TO BE DELETED ---------------- for(int i = f+1; i <= 200; i++) { if(a[i][f] != 0 && a[i][f] != -1) { a[i][s] += a[i][f]; for(int j = 0; j < v.size(); j++) { if (v[j].first == i && v[j].second == f ) { if(find(v.begin(), v.end(), make_pair(i, s) ) == v.end() ){ v[j] = make_pair(i, s); } else { v[j] = v[v.size()-1]; v.pop_back(); } break; } } } a[i][f] = -1; } //---------------- PROCESS VERTICES SMALLER THAN THE ONE TO BE DELETED ---------------- for(int i = 1; i < f; i++){ if (a[f][i] != 0 && a[f][i] != -1 && i != s){ if(i > s){ a[i][s] += a[f][i]; } else if ( i < s){ a[s][i] += a[f][i]; } for(int j = 0; j < v.size(); j++){ if (v[j].first == f && v[j].second == i) { if(i > s && find(v.begin(), v.end(), make_pair(i, s)) == v.end() ){ v[j] = make_pair(i, s); } else if (i < s && find(v.begin(), v.end(), make_pair(s, i)) == v.end() ){ v[j] = make_pair(s, i); } } } } if(i != s ) a[f][i] = -1; } a[f][s] = -1; } for(int i = 1; i <= 200; i++) { for(int j = 1; j <= 200; j++) { if(a[i][j] !=0 && a[i][j] != -1) cout << a[i][j]; } cout << "\n"; } cout << v[0].first << " " << v[0].second << "\n"; cout << a[v[0].first][v[0].second]; } /* if(a[1][1]> 0){ cout << "a[1][1]: " << to_string(a[1][1]) << " i: " << to_string(i) << " f: " << to_string(f) << " s: " << to_string(s) << "\n"; } */ //cout << to_string(f) << " " << to_string(s) << "\n"; //cout << "\n\n" << v[0].first << " " << v[0].second; /* for(int i = 1; i <= 200; i++) { for(int j = 1; j <= 200; j++) { cout << a[i][j]; } cout << "\n"; } */ //for(int i = 0; i < v.size(); i++) cout << v[i].first << " " << v[i].second << "\n"; //cout << f << " " << s << "\n";
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}