-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
AdvancedLevel_C++/1148. Werewolf - Simple Version (20).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <iostream> | ||
#include <cmath> | ||
#include <vector> | ||
using namespace std; | ||
int main() { | ||
int n; | ||
cin >> n; | ||
vector<int> v(n+1); | ||
for (int i = 1; i <= n; i++) cin >> v[i]; | ||
for (int i = 1; i <= n; i++) { | ||
for (int j = i + 1; j <= n; j++) { | ||
vector<int> lie, a(n + 1, 1); | ||
a[i] = a[j] = -1; | ||
for (int k = 1; k <= n; k++) | ||
if (v[k] * a[abs(v[k])] < 0) lie.push_back(k); | ||
if (lie.size() == 2 && a[lie[0]] + a[lie[1]] == 0) { | ||
cout << i << " " << j; | ||
return 0; | ||
} | ||
} | ||
} | ||
cout << "No Solution"; | ||
return 0; | ||
} |
28 changes: 28 additions & 0 deletions
28
AdvancedLevel_C++/1149. Dangerous Goods Packaging (25).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <map> | ||
using namespace std; | ||
int main() { | ||
int n, k, t1, t2; | ||
map<int,vector<int>> m; | ||
scanf("%d%d", &n, &k); | ||
for (int i = 0; i < n; i++) { | ||
scanf("%d%d", &t1, &t2); | ||
m[t1].push_back(t2); | ||
m[t2].push_back(t1); | ||
} | ||
while (k--) { | ||
int cnt, flag = 0, a[100000] = {0}; | ||
scanf("%d", &cnt); | ||
vector<int> v(cnt); | ||
for (int i = 0; i < cnt; i++) { | ||
scanf("%d", &v[i]); | ||
a[v[i]] = 1; | ||
} | ||
for (int i = 0; i < v.size(); i++) | ||
for (int j = 0; j < m[v[i]].size(); j++) | ||
if (a[m[v[i]][j]] == 1) flag = 1; | ||
printf("%s\n",flag ? "No" :"Yes"); | ||
} | ||
return 0; | ||
} |
49 changes: 49 additions & 0 deletions
49
AdvancedLevel_C++/1150. Travelling Salesman Problem (25).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <set> | ||
using namespace std; | ||
int e[300][300], n, m, k, ans = 99999999, ansid; | ||
vector<int> v; | ||
void check(int index) { | ||
int sum = 0, cnt, flag = 1; | ||
scanf("%d", &cnt); | ||
set<int> s; | ||
vector<int> v(cnt); | ||
for (int i = 0; i < cnt; i++) { | ||
scanf("%d", &v[i]); | ||
s.insert(v[i]); | ||
} | ||
for (int i = 0; i < cnt - 1; i++) { | ||
if(e[v[i]][v[i+1]] == 0) flag = 0; | ||
sum += e[v[i]][v[i+1]]; | ||
} | ||
if (flag == 0) { | ||
printf("Path %d: NA (Not a TS cycle)\n", index); | ||
} else if(v[0] != v[cnt-1] || s.size() != n) { | ||
printf("Path %d: %d (Not a TS cycle)\n", index, sum); | ||
} else if(cnt != n + 1) { | ||
printf("Path %d: %d (TS cycle)\n", index, sum); | ||
if (sum < ans) { | ||
ans = sum; | ||
ansid = index; | ||
} | ||
} else { | ||
printf("Path %d: %d (TS simple cycle)\n", index, sum); | ||
if (sum < ans) { | ||
ans = sum; | ||
ansid = index; | ||
} | ||
} | ||
} | ||
int main() { | ||
scanf("%d%d", &n, &m); | ||
for (int i = 0; i < m; i++) { | ||
int t1, t2, t; | ||
scanf("%d%d%d", &t1, &t2, &t); | ||
e[t1][t2] = e[t2][t1] = t; | ||
} | ||
scanf("%d", &k); | ||
for (int i = 1; i <= k; i++) check(i); | ||
printf("Shortest Dist(%d) = %d\n", ansid, ans); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <map> | ||
using namespace std; | ||
map<int, int> pos; | ||
vector<int> in, pre; | ||
void lca(int inl, int inr, int preRoot, int a, int b) { | ||
if (inl > inr) return; | ||
int inRoot = pos[pre[preRoot]], aIn = pos[a], bIn = pos[b]; | ||
if (aIn < inRoot && bIn < inRoot) | ||
lca(inl, inRoot-1, preRoot+1, a, b); | ||
else if ((aIn < inRoot && bIn > inRoot) || (aIn > inRoot && bIn < inRoot)) | ||
printf("LCA of %d and %d is %d.\n", a, b, in[inRoot]); | ||
else if (aIn > inRoot && bIn > inRoot) | ||
lca(inRoot+1, inr, preRoot+1+(inRoot-inl), a, b); | ||
else if (aIn == inRoot) | ||
printf("%d is an ancestor of %d.\n", a, b); | ||
else if (bIn == inRoot) | ||
printf("%d is an ancestor of %d.\n", b, a); | ||
} | ||
int main() { | ||
int m, n, a, b; | ||
scanf("%d %d", &m, &n); | ||
in.resize(n + 1), pre.resize(n + 1); | ||
for (int i = 1; i <= n; i++) { | ||
scanf("%d", &in[i]); | ||
pos[in[i]] = i; | ||
} | ||
for (int i = 1; i <= n; i++) scanf("%d", &pre[i]); | ||
for (int i = 0; i < m; i++) { | ||
scanf("%d %d", &a, &b); | ||
if (pos[a] == 0 && pos[b] == 0) | ||
printf("ERROR: %d and %d are not found.\n", a, b); | ||
else if (pos[a] == 0 || pos[b] == 0) | ||
printf("ERROR: %d is not found.\n", pos[a] == 0 ? a : b); | ||
else | ||
lca(1, n, 1, a, b); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <algorithm> | ||
using namespace std; | ||
int main() { | ||
int a, b; | ||
scanf("%d %d", &a, &b); | ||
string s = to_string(a * b); | ||
reverse(s.begin(), s.end()); | ||
printf("%d", stoi(s)); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <iostream> | ||
#include <set> | ||
using namespace std; | ||
int main() { | ||
int n; | ||
scanf("%d", &n); | ||
set<int> s; | ||
for (int i = 1; i <= n; i++) | ||
s.insert(i / 2 + i / 3 + i / 5); | ||
printf("%d", s.size()); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <cmath> | ||
using namespace std; | ||
int main() { | ||
int n; | ||
cin >> n; | ||
vector<int> v(n+1); | ||
for (int i = 1; i <= n; i++) cin >> v[i]; | ||
for (int i = 1; i <= n; i++) { | ||
for (int j = i + 1; j <= n; j++) { | ||
vector<int> lie, a(n + 1, 1); | ||
a[i] = a[j] = -1; | ||
for (int k = 1; k <= n; k++) | ||
if (v[k] * a[abs(v[k])] < 0) lie.push_back(k); | ||
if (lie.size() == 2 && a[lie[0]] + a[lie[1]] == 0) { | ||
cout << i << " " << j; | ||
return 0; | ||
} | ||
} | ||
} | ||
cout << "No Solution"; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <map> | ||
using namespace std; | ||
int main() { | ||
int n, k, t1, t2; | ||
map<int,vector<int>> m; | ||
scanf("%d%d", &n, &k); | ||
for (int i = 0; i < n; i++) { | ||
scanf("%d%d", &t1, &t2); | ||
m[t1].push_back(t2); | ||
m[t2].push_back(t1); | ||
} | ||
while (k--) { | ||
int cnt, flag = 0, a[100000] = {0}; | ||
scanf("%d", &cnt); | ||
vector<int> v(cnt); | ||
for (int i = 0; i < cnt; i++) { | ||
scanf("%d", &v[i]); | ||
a[v[i]] = 1; | ||
} | ||
for (int i = 0; i < v.size(); i++) | ||
for (int j = 0; j < m[v[i]].size(); j++) | ||
if (a[m[v[i]][j]] == 1) flag = 1; | ||
printf("%s\n",flag ? "No" :"Yes"); | ||
} | ||
return 0; | ||
} |