Get All Coding Solutions
Want to Increase Your Ranking, You Can Pay ,
per Solution
π¬Send Screenshot : DM Here
Todays Free Solutions π
Note: If you share our solution with others, we will not be held responsible for any plagiarism issues that may arise, and we will not provide any assistance in such cases.
NOTE :- Donβt copy the full code β use the logic.
We're not responsible for plagiarism issues For FREE SOLUTION.
Thank you
Q1Solution :
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Smax;
if(!(cin >> N >> Smax)) return 0;
vector<char> id(N);
vector<int> E(N), R(N);
for(int i = 0; i < N; i++){
cin >> id[i] >> E[i] >> R[i];
}
vector<vector<int>> dp(N+1, vector<int>(Smax+1, 0));
for(int i = 1; i <= N; i++){
for(int w = 0; w <= Smax; w++){
dp[i][w] = dp[i-1][w];
if(w >= E[i-1])
dp[i][w] = max(dp[i][w], dp[i-1][w - E[i-1]] + R[i-1]);
}
}
int bestReward = dp[N][Smax];
if(bestReward == 0){
cout << -1 << "\n";
return 0;
}
vector<int> chosen;
int w = Smax;
for(int i = N; i >= 1; i--){
if(dp[i][w] != dp[i-1][w]){
// task i-1 was used
chosen.push_back(i-1);
w -= E[i-1];
}
}
reverse(chosen.begin(), chosen.end());
int totalEffort = 0;
for(int idx : chosen) totalEffort += E[idx];
for(size_t i = 0; i < chosen.size(); i++){
if(i) cout << ' ';
cout << id[chosen[i]];
}
cout << "\n";
cout << totalEffort << ' ' << bestReward << "\n";
return 0;
}
Q2 Solution :
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <climits>
using namespace std;
const int MAXN = 100005;
vector<pair<int, int>> adj[MAXN];
int nodeValue[MAXN];
unordered_map<long long, int> edgeWeight;
long long getEdgeKey(int u, int v) {
return 1LL * min(u, v) * MAXN + max(u, v);
}
bool dfs(int curr, int target, vector<int>& path, vector<bool>& visited) {
visited[curr] = true;
path.push_back(curr);
if (curr == target) return true;
for (auto& [neighbor, _] : adj[curr]) {
if (!visited[neighbor]) {
if (dfs(neighbor, target, path, visited)) return true;
}
}
path.pop_back();
return false;
}
int weightedPathSum(int u, int v) {
vector<int> path;
vector<bool> visited(MAXN, false);
dfs(u, v, path, visited);
int minWeight = INT_MAX;
for (int i = 1; i < path.size(); ++i) {
int a = path[i - 1], b = path[i];
long long key = getEdgeKey(a, b);
minWeight = min(minWeight, edgeWeight[key]);
}
int sum = 0;
for (int node : path) {
sum += nodeValue[node] * minWeight;
}
return sum;
}
int main() {
int N, Q;
cin >> N >> Q;
for (int i = 1; i <= N; ++i)
cin >> nodeValue[i];
for (int i = 0; i < N - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
edgeWeight[getEdgeKey(u, v)] = w;
}
while (Q--) {
int type;
cin >> type;
if (type == 1) {
int x, val;
cin >> x >> val;
nodeValue[x] = val;
}
else if (type == 2) {
int u, v, w;
cin >> u >> v >> w;
edgeWeight[getEdgeKey(u, v)] = w;
for (auto& edge : adj[u])
if (edge.first == v) edge.second = w;
for (auto& edge : adj[v])
if (edge.first == u) edge.second = w;
}
else if (type == 3) {
int u, v;
cin >> u >> v;
cout << weightedPathSum(u, v) << endl;
}
}
return 0;
}
Q3 Solution :
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <climits>
using namespace std;
const int MAXN = 100005;
vector<pair<int, int>> adj[MAXN];
int nodeValue[MAXN];
unordered_map<long long, int> edgeWeight;
long long getEdgeKey(int u, int v) {
return 1LL * min(u, v) * MAXN + max(u, v);
}
bool dfs(int curr, int target, vector<int>& path, vector<bool>& visited) {
visited[curr] = true;
path.push_back(curr);
if (curr == target) return true;
for (auto& [neighbor, _] : adj[curr]) {
if (!visited[neighbor]) {
if (dfs(neighbor, target, path, visited)) return true;
}
}
path.pop_back();
return false;
}
int weightedPathSum(int u, int v) {
vector<int> path;
vector<bool> visited(MAXN, false);
dfs(u, v, path, visited);
int minWeight = INT_MAX;
for (int i = 1; i < path.size(); ++i) {
int a = path[i - 1], b = path[i];
long long key = getEdgeKey(a, b);
minWeight = min(minWeight, edgeWeight[key]);
}
int sum = 0;
for (int node : path) {
sum += nodeValue[node] * minWeight;
}
return sum;
}
int main() {
int N, Q;
cin >> N >> Q;
for (int i = 1; i <= N; ++i)
cin >> nodeValue[i];
for (int i = 0; i < N - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
edgeWeight[getEdgeKey(u, v)] = w;
}
while (Q--) {
int type;
cin >> type;
if (type == 1) {
int x, val;
cin >> x >> val;
nodeValue[x] = val;
}
else if (type == 2) {
int u, v, w;
cin >> u >> v >> w;
edgeWeight[getEdgeKey(u, v)] = w;
for (auto& edge : adj[u])
if (edge.first == v) edge.second = w;
for (auto& edge : adj[v])
if (edge.first == u) edge.second = w;
}
else if (type == 3) {
int u, v;
cin >> u >> v;
cout << weightedPathSum(u, v) << endl;
}
}
return 0;
}
telegram link not opening
ReplyDeleteshare WhatsApp
broooo, please give B
ReplyDeleteB karde yaar
ReplyDeletehello
ReplyDeletekisko answer chahie
ReplyDeleteHint dedo bus b ka ans nhi chahiye
Deleteb ka bta do
DeleteC dedo bas ek chahiye
ReplyDeletewhere B solutions?
ReplyDeleteneed a
ReplyDeleteneed a
ReplyDelete