Cod sursa(job #3330623)

Utilizator Andrei-Dani-10Pisla Andrei Daniel Andrei-Dani-10 Data 20 decembrie 2025 15:44:03
Problema Algoritmul Bellman-Ford Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>

#include <utility>
#define x first
#define y second

#include <vector>

using namespace std;

ifstream in("zaharel.in");
ofstream out("zaharel.out");

typedef pair <int, int> pii;
const int nmax = 1000;
int n, m, xx, yy;
int redd[nmax + 2];
int bluee[nmax + 2];

char color;

int where, visited[nmax + 2];
vector <pii> poly1, poly2;

int main(){

    in>>n>>m;
    for(int i = 1; i <= m; i++){
        in>>xx>>yy>>color;
        ///we are interested of one point because there is a point of color C on each line

        if(color == 'R'){ redd[xx] = yy; }
        if(color == 'A'){ bluee[yy] = xx; }
    }

    for(where = 1; !visited[where]; ){ ///merged polygon
        visited[where] = 1;
        where = bluee[redd[where]];
    }

    for(; visited[where]; ){ ///get the two polygons
        poly1.push_back(make_pair(where, redd[where]));
        poly2.push_back(make_pair(bluee[redd[where]], redd[where]));

        visited[where] = 0;
        where = bluee[redd[where]];
    }

    out<<poly1.size()<<"\n";
    for(auto f : poly1) out<<f.x<<" "<<f.y<<" "; out<<"\n";
    for(auto f : poly2) out<<f.x<<" "<<f.y<<" "; out<<"\n";

    return 0;
}