Submission #731643

# Submission time Handle Problem Language Result Execution time Memory
731643 2023-04-27T17:31:33 Z vjudge1 Političari (COCI20_politicari) C++14
70 / 70
22 ms 3412 KB
#include<iostream>
#include<vector>
#include<iomanip>
#include<algorithm>
#include<map>
#include<set>
#include<climits>
#include<numeric>
#include<queue>
#include<unordered_map>
#include<string>
#include<climits>
#include<unordered_set>
#include<bitset>
#include<cstring>
#include<cmath>
#include<stack>
#if 1
#define pb push_back
#define st first
#define nd second
#define endl '\n'
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MAXN ((int)(2e5+5))
#define int long long int
using namespace std;
const int inf = 1e17 + 5;
#endif

void fileio(string s){
    #ifndef ONLINE_JUDGE
        freopen((s+string(".in")).c_str(),"r",stdin);
        freopen((s+string(".out")).c_str(),"w",stdout);
    #endif
}


signed main(){
    //fileio("test");
    fio; 
    int n, k;
    cin >> n >> k;
    int a[n + 1][n + 1];
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            cin >> a[i][j];
        }
    }
    bool vis[n + 1][n + 1];
    memset(vis, false, sizeof(vis));
    vector<int>fst(2), cycle;
    int i = 1, cur = 2, prev = 1;
    fst[1] = vis[1][0] = 1;
    while(!vis[cur][prev]){
        vis[cur][prev] = true;
        fst.pb(cur);
        int new_cur = a[cur][prev];
        prev = cur;
        cur = new_cur;
    }
    memset(vis, false, sizeof(vis));
    while(!vis[cur][prev]){
        vis[cur][prev] = true;
        cycle.pb(cur);
        int new_cur = a[cur][prev];
        prev = cur;
        cur = new_cur;
    }
    int f = fst.size(), c = cycle.size();
    if(k < f){
        cout << fst[k] << endl;
    }
    else{
        k -= f;
        cout << cycle[k % c] << endl;
    }
    
}

/*

1 2 4 2 1 4 3

*/

Compilation message

politicari.cpp: In function 'int main()':
politicari.cpp:52:9: warning: unused variable 'i' [-Wunused-variable]
   52 |     int i = 1, cur = 2, prev = 1;
      |         ^
politicari.cpp: In function 'void fileio(std::string)':
politicari.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         freopen((s+string(".in")).c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
politicari.cpp:33:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |         freopen((s+string(".out")).c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 4 ms 976 KB Output is correct
3 Correct 11 ms 2256 KB Output is correct
4 Correct 14 ms 2772 KB Output is correct
5 Correct 22 ms 3408 KB Output is correct
6 Correct 17 ms 3412 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 4 ms 972 KB Output is correct
10 Correct 15 ms 2788 KB Output is correct
11 Correct 18 ms 3412 KB Output is correct
12 Correct 16 ms 3412 KB Output is correct
13 Correct 1 ms 276 KB Output is correct
14 Correct 2 ms 460 KB Output is correct