Submission #106308

#TimeUsernameProblemLanguageResultExecution timeMemory
106308xiaowuc1Tales of seafaring (POI13_mor)C++14
100 / 100
2071 ms102616 KiB
#include <algorithm>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef vector<vector<ll>> matrix;

vector<int> edges[5000];
short dp[5000][5000][2];
int n;

const short INF = 30000;

void init() {
  for(int s = 0; s < n; s++) {
    for(int t = 0; t < n; t++) {
      dp[s][t][0] = INF;
      dp[s][t][1] = INF;
    }
    queue<int> q;
    for(int out: edges[s]) {
      dp[s][out][1] = 1;
      q.push(out);
    }
    while(!q.empty()) {
      int t = q.front();
      q.pop();
      for(int out: edges[t]) {
        bool upd = false;
        if(dp[s][out][1] > dp[s][t][0] + 1) {
          dp[s][out][1] = dp[s][t][0] + 1;
          upd = true;
        }
        if(dp[s][out][0] > dp[s][t][1] + 1) {
          dp[s][out][0] = dp[s][t][1] + 1;
          upd = true;
        }
        if(upd) q.push(out);
      }
    }
  }
}

void solve() {
  int m, q;
  cin >> n >> m >> q;
  while(m--) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  init();
  while(q--) {
    int a, b, w;
    cin >> a >> b >> w;
    a--;
    b--;
    if(dp[a][b][w%2] == INF) cout << "NIE\n";
    else if(dp[a][b][w%2] <= w) cout << "TAK\n";
    else cout << "NIE\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
	/*
  int t;
  cin >> t;
  for(int i = 1; i <= t; i++) {
    cout << "Case #" << i << ": ";
    solve();
  }
	*/
	solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...