Can you convert the code into java?
#include<vector>
#include<iostream>
auto f=
[](auto m,int&r){
r=1; //set return flag to true
for(auto a:m) //for each element
for(auto b:m) //check with second element
if (a.second==b.first){ //do they chain?
int i=0; //flag for local transitivity
for(auto c:m) //search for a third element
i+=a.first==c.first&&b.second==c.second;
r*=i>0; //multiply with flag>0, resulting in 0 forever if one was not found
}
}
;
int main(){
std::vector<std::pair<int,int>> m={
{1, 2}, {2, 4}, {6, 5}, {1, 4}
};
int r;
f(m,r);
std::cout << r << std::endl;
m.emplace_back(3,6);
f(m,r);
std::cout << r << std::endl;
m.emplace_back(3,5);
f(m,r);
std::cout << r << std::endl;
}
import java.util.LinkedList;
public class Main {
public static boolean isTransitive(LinkedList<int[]> relations) {
for (int[] relationA : relations) {
for (int[] relationB : relations) {
if (relationA[1] == relationB[0]) {
boolean transitive = false;
for (int[] relationC : relations) {
if (relationC[0] == relationA[0] && relationC[1] == relationB[1]) {
transitive = true;
break;
}
}
if (!transitive) {
return false;
}
}
}
}
return true;
}
}
Comments
Leave a comment